如何将关键字参数传递给并发调用的函数

mar*_*t1n 5 python concurrency dictionary concurrent.futures

我有以下代码:

from concurrent.futures import ThreadPoolExecutor

def spam(url, hello=None, params=None):
    print(url, hello, params)

urls = [1, 2, 3, 4, 5]
params = [(6, 7), 7, ('a', 1), 9, 'ab']
with ThreadPoolExecutor(5) as executor:
    res = executor.map(spam, urls, params)
Run Code Online (Sandbox Code Playgroud)

预期打印:

1 (6, 7) None
2 7 None
3 ('a', 1) None
4 9 None
5 ab None
Run Code Online (Sandbox Code Playgroud)

有没有办法告诉map函数spam使用特定的关键字参数进行调用?在此示例中,我希望将值参数传递给hello参数,而不是下一行(在这种情况下为params)。

我要解决的实际用例是将params=值传递request.get给重复URL 的调用。

Jea*_*bre 7

你可以spam在做时包裹一个lambdamap

res = executor.map(lambda x,y:spam(x,params=y), urls, params)
Run Code Online (Sandbox Code Playgroud)