Python gevent:将多个参数传递给通过pool.map调用的函数

时间:2012-11-21 09:44:11

标签: python gevent

如何将多个参数传递给我通过pool.map调用的函数?

我的代码:

import gevent
from gevent.pool import Pool

def process(param1, param2):
    return "dosomething"

pool = Pool(10)
jobs = [('arg1a', 'arg1b'), ('arg2a', 'arg2b')]

# should pass arguments so that it results in these calls
# process(param1=arg1a, param2=arg1b)
# process(param1=arg2a, param2=arg2b)
results = pool.map(process, jobs) # does not work

1 个答案:

答案 0 :(得分:3)

gevent.Pool.map方法似乎只允许将单个项目作为参数传递给您提供的函数。但是,您可以编写函数来轻松解压缩其参数:

def process(params):
    param1, param2 = params
    ...

或者您可以在调用pool.map时使用lambda表达式解压缩参数:

results = pool.map(lambda args: process(*args), jobs)