threads and return values

Peter Hansen peter at engcorp.com
Sun May 5 21:33:05 EDT 2002


Adonis wrote:
> 
> if i push a function into a thread, how can i retrieve the value of the
> returning function?
> 
> any help would greatly be appreciated.

A thread never returns, so you have to store the value somewhere
and retrieve it from another thread, presumably the one that
created the thread in question in this case.

One good way to do this is with a Queue (see standard module
by that name).  Create a Queue and pass it in to the thread
when you create it, then wait for the thread to stick something
in it and get it out.  Alternatively, do a .join() on the
thread to wait for it to finish, then grab the data from
the Queue.  Or many such variations.

A simple approach I've used is simply to store the value as
an attribute of the thread object and then retrieve it 
after the thread terminates itself.

-Peter



More information about the Python-list mailing list