Say I have the following code:
def func(a,b):
return (func2(a),func3(3))
def paralel_func(alist,blist)
with ThreadPoolExecutor(max_workers=None) as executor:
executor.map(func,alist,blist)
How do I access the return values that were returned from func
? I tried to figure it out myself by thinking that executor.map
is a list that holds the values but that didnt work for me.
You can get results from the ThreadPoolExecutor in the order that tasks are completed by calling the as_completed() module function. The function takes a collection of Future objects and will return the same Future objects in the order that their associated tasks are completed.
ThreadPoolExecutor is an Executor subclass that uses a pool of threads to execute calls asynchronously. An Executor subclass that uses a pool of at most max_workers threads to execute calls asynchronously.
ThreadPoolExecutor is an ExecutorService to execute each submitted task using one of possibly several pooled threads, normally configured using Executors factory methods. It also provides various utility methods to check current threads statistics and control them.
I don't know where your alist and blist variables are coming from, but you could do something like this.
import concurrent.futures
def func(a, b):
return (func2(a),func3(3))
def parallel_func(alist, blist):
processes = []
with import concurrent.futures.ThreadPoolExecutor(max_workers=None) as executor:
processes.append(executor.submit(func, alist, blist))
for _ in concurrent.futures.as_completed(processes):
print('Result: ', _.result())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With