pythonmultithreadingpython-multithreadingexit-code

Return value from thread


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


Solution

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