我的主要问题是我真的不知道如何正确实现multiprocessing.queue,您不能为每个进程真正实例化对象,因为它们将是单独的队列,如何确保所有进程都与共享队列相关(或在这种情况下,排队)
这是读取器和写入器共享一个队列的简单示例。写入器向读取器发送一堆整数。当写入器的数字用完时,它将发送“ DONE”(完成),让读取器知道退出读取循环。
from multiprocessing import Process, Queueimport timeimport sysdef reader_proc(queue): ## Read from the queue; this will be spawned as a separate Process while True: msg = queue.get() # Read from the queue and do nothing if (msg == 'DONE'): breakdef writer(count, queue): ## Write to the queue for ii in range(0, count): queue.put(ii) # Write 'count' numbers into the queue queue.put('DONE')if __name__=='__main__': pqueue = Queue() # writer() writes to pqueue from _this_ process for count in [10**4, 10**5, 10**6]: ### reader_proc() reads from pqueue as a separate process reader_p = Process(target=reader_proc, args=((pqueue),)) reader_p.daemon = True reader_p.start() # Launch reader_proc() as a separate python process _start = time.time() writer(count, pqueue) # Send a lot of stuff to reader() reader_p.join() # Wait for the reader to finish print("Sending {0} numbers to Queue() took {1} seconds".format(count, (time.time() - _start)))
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)