In the previous discussion of stackoverflow:
python-multiprocessing-map-vs-map-async
As quikst3r say:You'll notice map will execute in order, but map_async doesn't.
Here is my example for map(block) vs map_async(non block).
map-sync.py code.
import multiprocessing
import os
import time
from datetime import datetime
def subprocess(number):
print('this is the %d th subprocess' % number)
time.sleep(3)
def mainprocess():
print('this is the main process ,process number is : %d' % os.getpid())
pool = multiprocessing.Pool(3)
list=range(9)
pool.map(subprocess,list)
pool.close()
pool.join()
if __name__ == '__main__':
mainprocess()
Sometimes map_sync can not execute in order for map function here.
map-async.py code.
import multiprocessing
import os
import time
from datetime import datetime
def subprocess(number):
print('this is the %d th subprocess' % number)
time.sleep(3)
def mainprocess():
print('this is the main process ,process number is : %d' % os.getpid())
pool = multiprocessing.Pool(3)
list=range(9)
pool.map_async(subprocess,list)
pool.close()
pool.join()
if __name__ == '__main__':
mainprocess()
Sometimes map_async can execute in order for map_async function here.
For multiprocession ,all process are Preemptive Multitasking,no order for map and map_async.
There is no absolute execution in order for map and map_async, and running time are almost same --9 seconds(3*3=9).
Let's see the block in apply function of multiprocessing module.
apply.py code.
import multiprocessing
import os
import time
from datetime import datetime
def subprocess(number):
print('this is the %d th subprocess' % number)
time.sleep(3)
def mainprocess():
print('this is the main process ,process number is : %d' % os.getpid())
pool = multiprocessing.Pool(3)
for i in range(9):
pool.apply(subprocess,args=(i,))
pool.close()
pool.join()
if __name__ == '__main__':
mainprocess()
27=3*9 (all processes blocked)
I am puzzled how to demonstrate the block and non-block attributions between map and map_async in multiprocessing module?
What are the differences between map(block) and map_async(non block) multiprocessing module?