How do I know when a thread quits?

Peter Hansen peter at engcorp.com
Tue Jun 7 09:41:16 EDT 2005


Prashanth Ellina wrote:
> I have used the low-level thread module to write a multi-threaded app.
> 
> tid = thread.start_new_thread(process, ())
> tid is an integer thread ident.
> 
> the thread takes around 10 seconds to finish processing. Meanwhile the
> main thread finished execution and exits. This causes an error because
> the child thread tries to access something in the sys module which has
> already  been GC'ed.  I want a reliable way of knowing when the child
> thread finished execution so that I can make the main thread wait till
> then.
> 
> Any ideas?

Yes, use the higher level "threading" module and either the isAlive() 
call on the Thread object, or just call join() on it to make the main 
thread wait till it finishes.  (By default the main thread will not exit 
until all threading Threads that aren't daemons have finished... maybe 
that's sufficient for your needs?)

-Peter



More information about the Python-list mailing list