Reputation: 2043
I have a small toy example like this
def hello(filename,suffix):
print(filename + str(suffix))
params = [('test.csv', 1), ('test2.csv', 2)]
ex = futures.ThreadPoolExecutor(max_workers=2)
results = ex.map(hello, params)
real_results = list(results)
When i execute this piece of code i get an error saying
TypeError: hello() missing 1 required positional argument: 'suffix'
Well i thought suffix is 1 in the first thread and 2 in the second thread.
What am i doing wrong here ?
Upvotes: 1
Views: 1462
Reputation:
Through ex.map
, the elements from params
will be passed as first argument to hello
. If you want to pass multiple arguments to a threaded function, there are a few possibilities:
Make two argument lists, one for first arguments, one for second arguments and pass them both as argument to exp.map
:
params1 = ['test.csv', 'test2.csv']
params2 = [1, 2]
results = ex.map(hello, params1, params2)
Let hello
take a single tuple of arguments and then unpack them:
def hello(args):
filename, suffix = args
Transpose and unpack the params
list onto ex.map
(really this is the same as 1. without manual coversion to params1
and params2
):
ex.map(hello, *zip(*params))
Upvotes: 5