【发布时间】:2020-08-20 15:54:03
【问题描述】:
为什么输出显示池在实际处理时已关闭?
res.get() 是阻止多处理的阻塞操作吗?
为什么会打印Now the pool is closed and no longer available 5 次?
from multiprocessing import Pool, TimeoutError
import time
import os
def f(x):
time.sleep(5)
return x*x
if __name__ == '__main__':
# start 4 worker processes
with Pool(processes=4) as pool:
for i in range(5):
# evaluate "f(20)" asynchronously
res = pool.apply_async(f, (20,)) # runs in *only* one process
print(res.get())
# exiting the 'with'-block has stopped the pool
print("Now the pool is closed and no longer available")
输出:
Now the pool is closed and no longer available
Now the pool is closed and no longer available
Now the pool is closed and no longer available
Now the pool is closed and no longer available
400
400
400
400
400
Now the pool is closed and no longer available
【问题讨论】:
标签: python python-3.x python-multiprocessing