在計算器的前面的討論: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(非塊)多處理模塊之間有什麼區別?