multiprocessing: map vs map_async multiprocessing: map vs map_async python python

multiprocessing: map vs map_async


There are four choices to mapping jobs to processes. You have to consider multi-args, concurrency, blocking, and ordering. map and map_async only differ with respect to blocking. map_async is non-blocking where as map is blocking

So let's say you had a function

from multiprocessing import Poolimport timedef f(x):    print x*xif __name__ == '__main__':    pool = Pool(processes=4)    pool.map(f, range(10))    r = pool.map_async(f, range(10))    # DO STUFF    print 'HERE'    print 'MORE'    r.wait()    print 'DONE'

Example output:

01941625364964810HERE14MORE1625369496481DONE

pool.map(f, range(10)) will wait for all 10 of those function calls to finish so we see all the prints in a row.r = pool.map_async(f, range(10)) will execute them asynchronously and only block when r.wait() is called so we see HERE and MORE in between but DONE will always be at the end.