如何将关键字参数传递给 concurrent.futures 地图调用调用的函数

How to pass keyword argument to function called by concurrent.futures map call

我有以下代码:

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)

这应该打印:

1 (6, 7) None
2 7 None
3 ('a', 1) None
4 9 None
5 ab None

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

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

您可以在执行 map

时将 spam 包装在 lambda 中
res = executor.map(lambda x,y:spam(x,params=y), urls, params)