Return value from thread

Paul Joseph picture Paul Joseph · Dec 11, 2009 · Viewed 85.5k times · Source

How do I get a thread to return a tuple or any value of my choice back to the parent in Python?

Answer

Alex Martelli picture Alex Martelli · Dec 11, 2009

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!-)