2017-08-29 4 views
1

:あなたは、マップは順番に実行されます気づくが、map_asyncないよ:quikst3rが言うようにpythonマルチプロセッシングモジュールのmap(block)とmap_async(non block)の違いは何ですか? stackoverflowの以前の議論では

python-multiprocessing-map-vs-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() 

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 = 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_async(非ブロック)マルチプロセッシングモジュールの違いは何ですか?

答えて

1

あなたはquikst3rの言葉を誤解するかもしれません。

プールは、ブロックについて何かを行う多くのワーカープロセスを作成します。ワーカープロセスがメインプロセスをブロックするかどうかを示します。 mapはメインプロセスutilをブロックして作業を終了し、map_asycはメインプロセスをブロックしないので、map_asyncではすべてのプロセスが同時に進行します。彼らは、ワーカープロセスの順序を保証しません。

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であれば、出力は次のようになります:それはマップがある場合

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

、出力は次のようになります

が適用されますかapply_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 

、それはですブロックについての意味は、mapまたはmap_asyncと同じです。これは、ワーカープロセスがメインプロセスをブロックするかどうかを示しています。

関連する問題