Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't map_async() need pool.close() and pool.join()?

I wrote the following code

import multiprocessing as mp
import time


#
def f(x) :
    time.sleep(0.1)
    return pow( x, 2 )

#
my_chunksize = 10

#
if __name__ == '__main__':

    #
    po = mp.Pool( processes=2 )
    po_res = po.map_async( f, range(100), my_chunksize )

    #po.close()
    #po.join()

    #
    print po_res.get()

This is working fine. Why are the po.close() and po.join() unneeded? Why doesn't the main process die before the children processes?

like image 717
usual me Avatar asked Jun 05 '26 15:06

usual me


1 Answers

Your final:

print po_res.get()

statement blocks the main program until the map_async() is entirely finished. It's the .get() that blocks, waiting for the entire result list to be available. And that can't happen until all the work passed to map_async() has been completed and all the results returned to the main program.

Comment that statement out, and you'll see that your program exits very quickly.

That said, it's best practice to use .close() and .join() regardless.

like image 168
Tim Peters Avatar answered Jun 07 '26 23:06

Tim Peters



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!