在计算器的前面的讨论:python多处理模块中的map(block)和map_async(非block)有什么不同?
python-multiprocessing-map-vs-map-async
由于quikst3r说:你会注意到地图会顺序执行,但map_async没有。
这是我的地图(块)vs map_async(非块)的例子。
map-sync.py代码。
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()
map-async.py代码。
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()
有时map_async可以为了执行在这里为map_async功能。
对于多进程,所有进程都是抢先式多任务处理,无需执行map和map_async命令。
地图和map_async没有绝对执行,运行时间几乎相同--9秒(3 * 3 = 9)。
我们来看看多处理模块应用功能块。
apply.py代码。
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(所有进程阻塞)
我困惑如何证明在多处理模块地图和map_async之间的块和非块归因?
map(块)和map_async(非块)多处理模块之间有什么区别?