2017-08-29 191 views
1

在計算器的前面的討論: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() 

Sometimes map_sync can not execute in order.
有時map_sync不能爲了執行在地圖的功能在這裏。

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() 

Sometimes map_async can execute in order. 有時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() 

enter image description here

27 = 3 * 9(所有進程阻塞)

我困惑如何證明在多處理模塊地圖和map_async之間的塊和非塊歸因?
map(塊)和map_async(非塊)多處理模塊之間有什麼區別?

回答

1

也許你誤解了quikst3r的話。

池創建許多工作進程來做某件事情,關於是否阻塞,它表示工作進程是否會阻塞主進程。並且map將阻止主進程util完成其工作,並且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() 

如果是地圖,輸出將是這樣的:

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_async,這是關於塊的含義與map或map_async相同。這是說工人進程阻止了主流程。

相關問題