In multithreading in python, how to yield for result and return thread value?

Viewed 727

Running this code, I get the following log and instead of future value, I get thread object. What is the right way to yield for result of f and finally return Y with values ?

Y = []
process_pool = ThreadPoolExecutor(4)
f = partial(some_func, *args)
for i in range(1000):
    future = process_pool.submit( f ,i)
    print(future)
    Y.append(future)
return Y 

log:

<Future at 0x7f82c8ebf390 state=running>
<Future at 0x7f82cb636f10 state=running>
<Future at 0x7f82cb0af090 state=running>
<Future at 0x7f82cb0af190 state=running>
<Future at 0x7f82cb0af350 state=pending>
<Future at 0x7f82cb0ae350 state=pending>
...

Y:

[<Future at 0x7f82c8ebf390 state=finished raised NameError>, <Future at 0x7f82cb636f10 state=finished raised NameError>, <Future at 0x7f82cb0af090 state=finished raised NameError>, <Future at 0x7f82cb0af190 state=finished raised NameError>, <Future at 0x7f82cb0af350 state=finished raised NameError>, <Future at 0x7f82cb0ae350 state=finished raised NameError>, <Future at 0x7f82cb0ae290 state=running>, <Future at 0x7f82cb0aea10 state=finished raised NameError>, <Future at 0x7f82cb0ae950 state=running>,...]

1 Answers

Two simple ways are: 1- make your f() store the result in a class or class instance variable; 2- use concurrent.futures.as_completed()

import concurrent.futures

Y = []
l_tasks = [] 
with concurrent.futures.ThreadPoolExecutor() as executor:
    for i in range(1000):
        l_tasks.append(executor.submit(f, i))
    for task in concurrent.futures.as_completed(l_tasks):
        Y.append(task.result())
Related