python多处理队列不在共享内存中

时间:2017-08-24 08:00:23

标签: python python-multiprocessing

我尝试运行以下代码:

import multiprocessing
import time

def init_queue():
    print("init g_queue start")
    while not g_queue.empty():
        g_queue.get()
    for _index in range(10):
        g_queue.put(_index)
    print("init g_queue end")
    return

def task_io(task_id):
    print("IOTask[%s] start" % task_id)
    print("the size of queue is %s" % g_queue.qsize())
    while not g_queue.empty():
        time.sleep(1)
        try:
            data = g_queue.get(block=True, timeout=1)
            print("IOTask[%s] get data: %s" % (task_id, data))
        except Exception as excep:
            print("IOTask[%s] error: %s" % (task_id, str(excep)))
    print("IOTask[%s] end" % task_id)
    return

g_queue = multiprocessing.Queue()

if __name__ == '__main__':
    print("the size of queue is %s" % g_queue.qsize())
    init_queue()
    print("the size of queue is %s" % g_queue.qsize())
    time_0 = time.time()
    process_list = [multiprocessing.Process(target=task_io, args=(i,)) for i in range(multiprocessing.cpu_count())]
    for p in process_list:
        p.start()
    for p in process_list:
        if p.is_alive():
            p.join()
    print("End:", time.time() - time_0, "\n")

我得到的是以下内容:

the size of queue is 0
init g_queue start
init g_queue end
the size of queue is 10
IOTask[0] start
the size of queue is 0
IOTask[0] end
IOTask[1] start
the size of queue is 0
IOTask[1] end
('End:', 0.6480000019073486, '\n')

我期待的是

IOTask[0] start
the size of queue is 10

因为在初始化g_queue之后,队列的大小应该是10,而不是0.看起来队列不在共享内存中。子进程启动时,会创建一个g_queue副本,其大小为0.

为什么multiprocessing.queue不在共享内存中?请指教。非常感谢!

1 个答案:

答案 0 :(得分:2)

您应该将g_queue作为参数传递,然后它才能正常工作。

使用队列

进行多处理的演示
import multiprocessing
import time


def long_time_calculate(n, result_queue):
 time.sleep(1)
 result_queue.put(n)


if __name__ == '__main__':
 result_queue = multiprocessing.Queue()
 pool_size = multiprocessing.cpu_count() * 2
 pool = multiprocessing.Pool(processes=pool_size, maxtasksperchild=4)

 manager = multiprocessing.Manager()
 result_queue = manager.Queue()

 inputs = [(1, result_queue), (2, result_queue), (3, result_queue), (4, result_queue)]

 for input in inputs:
     pool.apply_async(long_time_calculate, input)

 pool.close()
 pool.join()

 print(list(result_queue.get() for _ in inputs))