为什么这个 multiprocessing.Pool 卡住了?
Why is this multiprocessing.Pool stuck?
代码:
from multiprocessing import Pool
print ('parent')
max_processes = 4
def foo(result):
print (result)
def main():
pool = Pool(processes=max_processes)
while True:
pool.apply_async(foo, 5)
if __name__ == '__main__':
main()
'parent' 被打印了 5 次,因此创建了初始池。但是没有执行 print(result) 语句。
尝试添加with Pool(processes=max_processes) as pool:
with Pool(processes=max_processes) as pool:
while True:
pool.apply_async(foo, 5)
...
Warning multiprocessing.pool objects have internal resources that need to be properly managed (like any other resource) by using the pool as a context manager or by calling close() and terminate() manually. Failure to do this can lead to the process hanging on finalization.
您在调用 apply_async
时传递的参数不正确。参数需要在一个元组中(或者其他序列,也许),但你将 5
作为一个裸数字传递。
尝试:
def main():
pool = Pool(processes=max_processes)
while True:
pool.apply_async(foo, (5,)) # make a 1-tuple for the args!
代码:
from multiprocessing import Pool
print ('parent')
max_processes = 4
def foo(result):
print (result)
def main():
pool = Pool(processes=max_processes)
while True:
pool.apply_async(foo, 5)
if __name__ == '__main__':
main()
'parent' 被打印了 5 次,因此创建了初始池。但是没有执行 print(result) 语句。
尝试添加with Pool(processes=max_processes) as pool:
with Pool(processes=max_processes) as pool:
while True:
pool.apply_async(foo, 5)
...
Warning multiprocessing.pool objects have internal resources that need to be properly managed (like any other resource) by using the pool as a context manager or by calling close() and terminate() manually. Failure to do this can lead to the process hanging on finalization.
您在调用 apply_async
时传递的参数不正确。参数需要在一个元组中(或者其他序列,也许),但你将 5
作为一个裸数字传递。
尝试:
def main():
pool = Pool(processes=max_processes)
while True:
pool.apply_async(foo, (5,)) # make a 1-tuple for the args!