passing kwargs with multiprocessing.pool.map

后端 未结 2 1157
天涯浪人
天涯浪人 2020-12-24 08:24

I would like to pass keyword arguments to my worker-function with Pool.map(). I can\'t find a clear example of this when searching forums.

Example Code:

<         


        
2条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-24 08:41

    If you want to iterate over the other arguments, use @ArcturusB's answer.

    If you just want to pass them, having the same value for each iteration, then you can do this:

    from functools import partial
    pool.map(partial(worker, **kwargs), jobs)
    

    Partial 'binds' arguments to a function. Old versions of Python cannot serialize partial objects though.

提交回复
热议问题