Home > Net >  Why is this multiprocessing.Pool stuck?
Why is this multiprocessing.Pool stuck?

Time:02-03

CODE:

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' gets printed 5 times, so initial pools were created. But there was no execution from print(result) statement.

CodePudding user response:

You are passing the arguments incorrectly in your call to apply_async. The arguments need to be in a tuple (or other sequence, maybe), but you're passing 5 as a bare number.

Try:

def main():
    pool = Pool(processes=max_processes)
    while True:
        pool.apply_async(foo, (5,)) # make a 1-tuple for the args!

CodePudding user response:

Try to add 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.

  •  Tags:  
  • Related