2016-10-06 3 views
1

Win 7、x64、Python 2.7.12pool.mapプロセスから複数のリストを返す?

次のコードでは、multiprocessing.Pool.map()メソッドを使用して些細な乗算を行うプールプロセスをいくつか設定しています。出力データはList_1に集められます。

注:これは私の実際のコードを簡略化したものです。実際のアプリケーションには複数のリストが含まれています。私は今createLists() &に余分な計算を追加

import multiprocessing 
import numpy as np 

def createLists(branches): 

    firstList = branches[:] * node 

    return firstList 


def init_process(lNodes): 

    global node 
    node = lNodes 
    print 'Starting', multiprocessing.current_process().name 


if __name__ == '__main__': 

    mgr = multiprocessing.Manager() 
    nodes = mgr.list() 
    pool_size = multiprocessing.cpu_count() 

    branches = [i for i in range(1, 21)] 
    lNodes = 10 
    splitBranches = np.array_split(branches, int(len(branches)/pool_size)) 

    pool = multiprocessing.Pool(processes=pool_size, initializer=init_process, initargs=[lNodes]) 
    myList_1 = pool.map(createLists, splitBranches) 

    pool.close() 
    pool.join() 

は、バック両方のリストを渡すためにしてみてください。

import multiprocessing 
import numpy as np 

def createLists(branches): 

    firstList = branches[:] * node 
    secondList = branches[:] * node * 2 

    return firstList, secondList 


def init_process(lNodes): 
    global node 
    node = lNodes 
    print 'Starting', multiprocessing.current_process().name 


if __name__ == '__main__': 

    mgr = multiprocessing.Manager() 
    nodes = mgr.list() 
    pool_size = multiprocessing.cpu_count() 

    branches = [i for i in range(1, 21)] 
    lNodes = 10 
    splitBranches = np.array_split(branches, int(len(branches)/pool_size)) 

    pool = multiprocessing.Pool(processes=pool_size, initializer=init_process, initargs=[lNodes]) 
    myList_1, myList_2 = pool.map(createLists, splitBranches) 

    pool.close() 
    pool.join() 

これは以下のエラー&トレースバックを上げる..私は戻っすなわち渡すために一つに両方のリストを入れてみました

Traceback (most recent call last): 

    File "<ipython-input-6-ff188034c708>", line 1, in <module> 
    runfile('C:/Users/nr16508/Local Documents/Inter Trab Angle/Parallel/scratchpad.py', wdir='C:/Users/nr16508/Local Documents/Inter Trab Angle/Parallel') 

    File "C:\Users\nr16508\AppData\Local\Continuum\Anaconda2\lib\site-packages\spyder\utils\site\sitecustomize.py", line 866, in runfile 
    execfile(filename, namespace) 

    File "C:\Users\nr16508\AppData\Local\Continuum\Anaconda2\lib\site-packages\spyder\utils\site\sitecustomize.py", line 87, in execfile 
    exec(compile(scripttext, filename, 'exec'), glob, loc) 

    File "C:/Users/nr16508/Local Documents/Inter Trab Angle/Parallel/scratchpad.py", line 36, in <module> 
    myList_1, myList_2 = pool.map(createLists, splitBranches) 

ValueError: too many values to unpack 

...

return [firstList, secondList] 
...... 
myList = pool.map(createLists, splitBranches) 

...より多くの処理のために出力が混乱してしまいます。

プールされたプロセスから複数のリストを収集する方法はありますか?

答えて

1

この質問は、マルチプロセッシングやスレッドプーリングとは関係ありません。リストを解凍する方法については、標準のzip(*...)イディオムで行うことができます。

myList_1, myList_2 = zip(*pool.map(createLists, splitBranches)) 
関連する問題