how can I tell when a Thread object dies?

David Bolen db3l at fitlinxx.com
Tue Nov 7 20:24:20 EST 2000


yinger <ayinger1 at my-deja.com> writes:

> ok, well, I tried that:
> (changed code to return 1 instead of sys.exit(1)) then...
> 
> returnValue = threadObject.run()

You don't want to call the run() method of your thread object directly
- it's designed to be run from within the separate thread after you
.start() the thread object.  When you return from it, you just shut
down that other thread.

> I can't use threadObject.join() because in some cases I don't want to
> join the thread.
> 
> I even tried making a custom Thread subclass, overriding __init__ and
> run methods ... but I still get None object.
> 
> So, how do I get return value from a running thread???

There are potentially different best approaches if you want a real
exit value (in which case the thread is no longer running) versus
retrieving some value from an operational thread.  The former is
easier since you probably have no need to worry about locking.

But in general, realize that the thread object still exists whether or
not there is a separate thread of execution currently executing the
run() method code from the thread object.  So you can add any methods
you want to a thread object to provide access to any of its state,
just as you would with any normal object.  Of course, if you may be
retrieving information while the thread is still running, you may need
to lock that thread data within the thread to prevent multiple threads
of execution from accessing it, depending on the circumstances.

For example, here's a small sample of a thread that just sleeps for 10
seconds, and then sets an exit code before terminating.  The exit code
is maintained as instance data in the thread object, and a method
provides access to that code at any time.  

The main application creates and starts the thread, then waits around
for it to exit, and then retrieves the status via the thread method.
I've used join() to wait for the thread, but you wouldn't have to
block, just identify that the thread had exited via any means.

This sort of method can also be used to poll a thread for information
(e.g., you could call the get_exit_code() method at any time), but as
noted above, if the thread object method affects thread state or
information also touched by the run() method from within the separate
thread, you should lock such accesses to prevent corruption.

    import time
    import threading

    class MyThread (threading.Thread):

	def __init__(self):
	    threading.Thread.__init__(self)
	    self.exit_code = 0

	def run(self):
	    print "Thread executing..."
	    time.sleep(10)
	    self.exit_code = 10
	    print "Thread terminating..."

	def get_exit_code(self):
	    return self.exit_code


    if __name__ == "__main__":

	threadobj = MyThread()
	print "Starting thread..."
	threadobj.start()
	print "Waiting for thread..."
	threadobj.join()
	print "Thread exit code = %d" % threadobj.get_exit_code()

When run, it shows:

    Starting thread...
    Thread executing...
    Waiting for thread...
    Thread terminating...
    Thread exit code = 10


--
-- David
-- 
/-----------------------------------------------------------------------\
 \               David Bolen            \   E-mail: db3l at fitlinxx.com  /
  |             FitLinxx, Inc.            \  Phone: (203) 708-5192    |
 /  860 Canal Street, Stamford, CT  06902   \  Fax: (203) 316-5150     \
\-----------------------------------------------------------------------/



More information about the Python-list mailing list