thread return code

R

Rajat

Hi,

I'm using threading module in Python 2.6.4. I'm using thread's join()
method.

On the new thread I'm running a function which returns a code at the
end. Is there a way I access that code in the parent thread after
thread finishes? Simply, does join() could get me that code?


Regards,
Rajat
 
A

Alf P. Steinbach

* Rajat:
Hi,

I'm using threading module in Python 2.6.4. I'm using thread's join()
method.

On the new thread I'm running a function which returns a code at the
end. Is there a way I access that code in the parent thread after
thread finishes? Simply, does join() could get me that code?

join() always returns None.

But you can store the code in the thread object, and then access it after the
join().


Cheers & hth.,

- Alf
 
P

Peter

* Rajat:




join() always returns None.

But you can store the code in the thread object, and then access it after the
join().

Cheers & hth.,

- Alf

The typical way to communicate with a thread is via a queue or pipe.
You can do what Alf suggests or you could create a queue (or pipe),
pass it to the thread as an argument and have the thread put the
"return value" into the queue as the last action prior to exit. After
the join() just read the results from the queue.

Using a queue or pipe is just a suggestion, the multiprocessing module
offers numerous ways to communicate between tasks, have a read and
pick whatever mechanism seems appropriate for your situation.

Peter
 
D

Dennis Lee Bieber

The typical way to communicate with a thread is via a queue or pipe.
You can do what Alf suggests or you could create a queue (or pipe),
pass it to the thread as an argument and have the thread put the
"return value" into the queue as the last action prior to exit. After
the join() just read the results from the queue.
If going the route of a return queue, why read the queue /after/ a
join(). One could return the thread ID and return code as a tuple, and
use the ID to perform the join() after reading the data from the queue.

Receiving such serves as the notification that the thread is ready
to be joined -- and if multiple threads with different run times are in
play, means faster response. Otherwise one may be waiting on a join()
for the longest running thread before it retrieves any results.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top