从返回多个列表池.map过程?

2024-10-01 15:35:34 发布

您现在位置:Python中文网/ 问答频道 /正文

Win 7,x64,Python 2.7.12

在下面的代码中,我将启动一些池进程,通过multiprocessing.Pool.map()方法进行一次简单的乘法。输出数据收集在List_1。在

注意:这是对我实际代码的简化。在实际的应用程序中涉及到多个列表,都非常庞大。在

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

我现在向createLists()添加一个额外的计算,并尝试将两个列表都传回。在

^{pr2}$

这将引发以下错误和回溯。。在

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)

…输出变得过于混乱,无法进行进一步处理。在

是否有一种从池进程收集多个列表的方法?在


Tags: inpymaplocallinemultiprocessingusersfile

热门问题