How to Get the Return Value from a Thread in Python

How to get the return value from a thread in Python?

In Python 3.2+, stdlib concurrent.futures module provides a higher level API to threading, including passing return values or exceptions from a worker thread back to the main thread:

import concurrent.futures

def foo(bar):
print('hello {}'.format(bar))
return 'foo'

with concurrent.futures.ThreadPoolExecutor() as executor:
future = executor.submit(foo, 'world!')
return_value = future.result()
print(return_value)

How to get return values of multi threads once one of them is finished? [closed]

Indeed, you can make the threads put their results into a queue (queue.put) while the main thread waits for results from the queue (queue.get). That's a thread-safe class by the way.

Official documentation: https://docs.python.org/3/library/queue.html

Return value in a python thread

In your setup that is a little difficult because you have no reference to the thread. So

  1. After starting it when is the thread ready?
  2. Where to are the results returned?

1 can be fixed by assigning the thread to a variable.
2 in your case you can provide an external list for the results and pass it as argument.

import threading
import time


def test_thread(list):

for i in range(10):
list.append(str(i))


my_list = []

my_thread = threading.Thread(target=test_thread, args=[my_list])
my_thread.start()

while my_thread.is_alive():
time.sleep(0.1)

print(my_list)

Result

['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']

Return value from thread

I suggest you instantiate a Queue.Queue before starting the thread, and pass it as one of the thread's args: before the thread finishes, it .puts the result on the queue it received as an argument. The parent can .get or .get_nowait it at will.

Queues are generally the best way to arrange thread synchronization and communication in Python: they're intrinsically thread-safe, message-passing vehicles -- the best way to organize multitasking in general!-)

Get return value from each loop of thread

I'd recommend using a queue to share data between threads. Pass the queue to the function (it will pass the reference not a copy of the queue, that's how Python works) then instead of return use the queue.put() method to place timer in the queue. Your other thread(s) can then retrieve the value using the queue.get() method.

https://docs.python.org/3/library/queue.html

Also is there a reason you're using not using the standard threading.Thread class? It's safer to use this unless you've got some very goof reason to be using the private methods.

Python: How to get multiple return values from a threaded function

You should use a queue for retrieve data from threads, here you have an example using a wrapper to store values from the functions into a queue:

import threading
import queue

my_queue = queue.Queue()

def storeInQueue(f):
def wrapper(*args):
my_queue.put(f(*args))
return wrapper


@storeInQueue
def get_name(full_name):
return full_name, full_name



t = threading.Thread(target=get_name, args = ("foo", ))
t.start()

my_data = my_queue.get()
print(my_data)

Here you have the live working example

Get return value from a function in thread in Python

You could try using the ThreadPool class

from multiprocessing.pool import ThreadPool
threadp = ThreadPool(processes=1)

res = threadp.apply_async(publishIAmFree, ()) # () has the arguments for function

return_val = res.get()
print return_val


Related Topics



Leave a reply



Submit