【发布时间】:2018-12-26 04:23:16
【问题描述】:
1 from multiprocessing import Pool, Manager
2
3
4 def test(num):
5 queue.put(num)
6
7
8 queue = Manager().Queue()
9 pool = Pool(5)
10
11 for i in range(30):
12 pool.apply_async(test, (i, ))
13
14 pool.close()
15 pool.join()
16
17 print(queue.qsize())
上面代码的输出是30。但是,如果将第8行与第9行交换(见下面的代码),输出将是0。那么有谁知道为什么?谢谢!
1 from multiprocessing import Pool, Manager
2
3
4 def test(num):
5 queue.put(num)
6
7
8 pool = Pool(5)
9 queue = Manager().Queue()
10
11 for i in range(30):
12 pool.apply_async(test, (i, ))
13
14 pool.close()
15 pool.join()
16
17 print(queue.qsize())
from multiprocessing import Process, Queue
def test():
queue.put(1)
p = Process(target=test)
queue = Queue()
p.start()
p.join()
print(queue.qsize())
输出为1,表示子进程将数字放入父进程创建的队列中。对吗?
【问题讨论】:
-
请去掉代码sn-ps旁边的行号。它们使人们很难复制粘贴您的代码进行尝试。
标签: python python-3.x queue multiprocessing pool