Why is this multiprocessing.Pool stuck?

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.

>Solution :

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!

Leave a Reply