Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I access the return values of ThreadPoolExecutor?

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.

like image 271
zython Avatar asked Apr 06 '18 09:04

zython


People also ask

How do you get results from ThreadPoolExecutor?

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.

How does ThreadPoolExecutor work in Python?

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.

What is ThreadPoolExecutor?

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.


1 Answers

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())
like image 148
NL23codes Avatar answered Oct 26 '22 23:10

NL23codes