6

I have the following code:

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)

This expectedly prints:

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

Is there a way to tell the map function to call spam with a particular keyword argument? In this example, I'd like the values params to be passed to the hello argument rather then the next in line (which in this case is params).

The real-world use case I'm trying to solve is passing the params= value to a request.get call for a repeating URL.

mart1n
  • 5,969
  • 5
  • 46
  • 83

1 Answers1

8

You could wrap spam in a lambda when doing map

res = executor.map(lambda x,y:spam(x,params=y), urls, params)
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219