On Tue, 07 Jun 2005 09:41:16 -0400, Peter Hansen wrote:
On Tue, 07 Jun 2005 06:28:33 -0700, Prashanth Ellina wrote:
Hi,
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.
Prashanth,
By reading the Python documentation I figured out to do the following:
import threading
# create an event flag to signal the completion of the thread
task_event=threading.Event()
# create thread
task_thread = threading.Thread\
(None, name_of_thread_function, None, (thread_args...))
# clear the wait for event flag
task_event.clear()
while ...:
# run thread
try:
task_thread.start()
except:
task_event.set()
error_handler()
if main thread has nothing to do:
# wait for thread to complete (wait on event flag)
task_event.wait()
else:
if task_event.isSet():
# thread has completed
else:
# thread is still active
# so main thread can continue with more tasks
##end while...
Remember to have the "task_thread" set the "task_event" flag prior to
completing.
Hope this helps.