在之前的stackoverflow讨论中:
python-multiprocessing-map-vs-map-async
正如quikst3r所说:你会注意到map会按顺序执行,但map_async不会。
这是我的map(block)vs map_async(non block)的例子。
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 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()
有时map_async可以在这里执行map_async函数。
对于多处理,所有进程都是抢先式多任务处理,没有map和map_async的顺序。
map和map_async没有绝对执行,运行时间几乎相同--9秒(3 * 3 = 9)。
让我们看一下多处理模块应用功能中的块。
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(所有进程都被阻止)
我很困惑如何在多处理模块中演示map和map_async之间的块和非块属性?
map(块)和map_async(非块)多处理模块之间有什么区别?
答案 0 :(得分:1)
也许你误解了quikst3r的话。
pool创建许多工作进程来执行某些操作,关于是否阻止,这意味着工作进程是否会阻止主进程。 map会阻止主进程使用它完成其工作,而map_asyc不会阻塞主进程,所以在map_async中所有进程都将同时进行。他们无法确保工人流程的顺序。
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) # or map_aysnc
print "hallo"
print "hallo again"
pool.close()
pool.join()
if __name__ == '__main__':
mainprocess()
如果是map,输出将如下:
this is the main process ,process number is : 24812
this is the 0 th subprocess
this is the 1 th subprocess
this is the 2 th subprocess
this is the 3 th subprocess
this is the 4 th subprocess
this is the 5 th subprocess
this is the 6 th subprocess
this is the 7 th subprocess
this is the 8 th subprocess
hallo
hallo again
如果是map_async,则输出为:
this is the main process ,process number is : 24775
hallo
hallo again
this is the 0 th subprocess
this is the 1 th subprocess
this is the 2 th subprocess
this is the 3 th subprocess
this is the 4 th subprocess
this is the 5 th subprocess
this is the 6 th subprocess
this is the 7 th subprocess
this is the 8 th subprocess
对于apply或apply_async,它对block的含义与map或map_async相同。它说工人进程是否阻止主要进程。