进程值之间的通信问题

1.用文件传递

import multiprocessing
import time
def write():
    a = open('abc.txt', 'w')
    for i in range(5):
        a.write(str(i))
    a.close()
def read():
    b=open('abc.txt','r')
    print(b.read())
    b.close()

if __name__ == '__main__':

    a1=multiprocessing.Process(target=write)
    a2=multiprocessing.Process(target=read)
    a1.start()
    a1.join()
    a2.start()


2.用队列传递
import multiprocessing
import time

queue = multiprocessing.Queue(10)
def write(queue):
    for i in range(5):
        if queue.full():
          print('man l ')
          break
        queue.put(i)
def read(queue):
    while True:
        if queue.empty():
            print('kong')
            break
        print(queue.get())

if __name__ == '__main__':
    queue = multiprocessing.Queue(10)
    a=multiprocessing.Process(target=write,args=(queue,))
    b=multiprocessing.Process(target=read,args=(queue,))
    a.start()
    a.join()
    b.start()

阅读更多
换一批

没有更多推荐了,返回首页