What's the difference between using map
and map_async
? Are they not running the same function after distributing the items from the list to 4 processes?
So is it wrong to presume both are running asynchronous and parallel?
def f(x):
return 2*x
p=Pool(4)
l=[1,2,3,4]
out1=p.map(f,l)
#vs
out2=p.map_async(f,l)
There are four choices to mapping jobs to processes. You have to consider multi-args, concurrency, blocking, and ordering.
map
andmap_asnyc
only differ with respect to blocking.map_async
is non-blocking where asmap
is blockingSo let's say you had a function
Example output:
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 whenr.wait()
is called so we seeHERE
andMORE
in between butDONE
will always be at the end.