您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

如何在Python中使用多处理队列?

如何在Python中使用多处理队列?

我的主要问题是我真的不知道如何正确实现multiprocessing.queue,您不能为每个进程真正实例化对象,因为它们将是单独的队列,如何确保所有进程都与共享队列相关(或在这种情况下,排队)

这是读取器和写入器共享一个队列的简单示例。写入器向读取器发送一堆整数。当写入器的数字用完时,它将发送“ DONE”(完成),让读取器知道退出读取循环。

from multiprocessing import Process, Queue
import time
import sys

def 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'):
            break

def 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)))
python 2022/1/1 18:47:01 有361人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶