Python multiprocessing - Passing a list of dicts to a pool

泄露秘密 提交于 2019-12-10 14:58:33

问题


This question may be a duplicate. However, I read lot of stuff around on this topic, and I didn't find one that matches my case - or at least, I didn't understood it.

Sorry for the inconvenance.

What I'm trying to do is fairly common, passing a list of kwargs to pool.starmap(), to achieve multiprocessing.

Here's my reduced case:

def compute(firstArg, **kwargs): # A function that does things
    # Fancy computing stuff...
    # Saving to disk...
    return True

if __name__ ==  '__main__':
    args = [{
        'firstArg': "some data",
        'otherArg': "some other data"
        'andAnotherArg': x*2
    } for x in range(42)]

    pool = Pool(4)
    pool.starmap(compute, args)
    pool.close()
    pool.terminate()

I supposed starmap() will unpack the dict and pass it to compute() as keyword args, but looking at the source code (see also l.46), it sends only keys (or values ?).

So it raises :

TypeError: compute() takes 1 positional argument but 3 were given

It must be a clear, straight forward way to do this... Any help would be appreciated.

Here's a quite similar question : Python Multiprocessing - How to pass kwargs to function?


回答1:


You could use a tiny proxy function:

def proxy(Dict):
    return compute(**Dict)

pool.map(proxy, args)

Or, since you don't need the proxy function polluting the namespace:

pool.map(lambda Dict: compute(**Dict), args)


来源:https://stackoverflow.com/questions/43957843/python-multiprocessing-passing-a-list-of-dicts-to-a-pool

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!