将参数和manager.dict传递给python 2.7中的多处理池

时间:2015-03-09 13:24:25

标签: python python-2.7 multiprocessing pool

我希望并行化一个函数,它将使用Pool而不是Process更新共享字典,这样我就不会过多地分配cpu。

即。我可以拿这个

def my_function(bar,results):
    results[bar] = bar*10

def paralell_XL():

    from multiprocessing import Pool, Manager, Process

    manager = Manager()
    results=manager.dict()

    jobs = []
    for bar in foo:
        p=Process(target=my_function, args=(bar, results))
        jobs.append(p)
        p.start()

    for proc in jobs:
        proc.join()

并将paralell_XL()函数更改为这样的东西?

def paralell_XL():

    from multiprocessing import Pool, Manager, Process

    manager = Manager()
    results=manager.dict()

    p = Pool(processes=4)
    p.map(my_function,(foo,results))

尝试以上操作会出现以下错误

TypeError: unsupported operand type(s) for //: 'int' and 'DictProxy'

感谢

1 个答案:

答案 0 :(得分:1)

所以问题在于将许多参数传递给池。正如此处Python multiprocessing pool.map for multiple arguments所示,您只需将其变为元组并添加包装器即可。这适用于传递manager.dict作为参数。

def my_function(bar,results):
    results[bar] = bar*10

def func_star(a_b):
    """Convert `f([1,2])` to `f(1,2)` call."""
    return my_function(*a_b)

def paralell_XL():

    from multiprocessing import Pool, Manager, Process
    import itertools

    manager = Manager()
    results=manager.dict()

    pool = Pool(processes=4)    
    pool.map(func_star, itertools.izip(foo, itertools.repeat(results)))

(注意我认为这个问题+答案是值得保留的,因为我不能完全清楚你能够以这种方式将manager.dict传递给函数)