jhon.smith
jhon.smith

Reputation: 2043

concurrent.futures errors out saying argument missing 1 required positional argument

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

Answers (1)

user10605163
user10605163

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:

  1. 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)
    
  2. Let hello take a single tuple of arguments and then unpack them:

    def hello(args):
        filename, suffix = args
    
  3. 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

Related Questions