Home > OS >  Get the parameters passed into function used a thread back
Get the parameters passed into function used a thread back

Time:09-21

I would like to be able to access the parameters passed into a function that has been used for threading to match up the the result of the function with the parameters passed in. Using ThreadPoolExecutor in Python.

data = [[url1, 1], [url2, 2], [url3, 3]]
lst = []

with concurrent.futures.ThreadPoolExecutor() as executor:
    results = [executor.submit(check, var1, var2) for var1, var2 in data]
    for f in concurrent.futures.as_completed(results):
        result = f.result()
        if result:
            lst.append([result, ????])

So I would like the '????' on the last line to be the element of the list 'data' corresponding to 'result'.

Cheers

CodePudding user response:

You use a dictionary with the key as the Future object and the value as the argument passed.

results = {executor.submit(check, var1, var2): (var1, var2) for var1, var2 in data}

Then when you want to refer just use something like lst.append([result, results[f]])

data = [[url1, 1], [url2, 2], [url3, 3]]
lst = []

with concurrent.futures.ThreadPoolExecutor() as executor:
    results = {executor.submit(check, var1, var2): (var1, var2) for var1, var2 in data}
    for f in concurrent.futures.as_completed(results.keys()):
        result = f.result()
        if result:
            lst.append([result, results[f]])
  • Related