How do I know when a thread quits?

  • Thread starter Prashanth Ellina
  • Start date
P

Prashanth Ellina

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.

Any ideas?

TIA,
Prashanth Ellina
 
P

Peter Hansen

Prashanth said:
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
 
H

harold fellermann

Hi,
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?

use a lock. the subthread allocates the lock and releases it after
processing.
the main thread must wait until the lock is released. otherwiese, use
the
higher level Threading module which provides a function Thread.join
that allows
the main thread to wait for the subthread. I think it uses the same
mechanism
that I explained above.

- harold -
 
D

dowskimania

I'm no threads expert, but if you use the higher-level "threading"
module, you could try something like this:

import threading
import time

def countToTen():
for i in range(1,11):
print i
time.sleep(0.5)

child = threading.Thread(target=countToTen)

class masterThread(threading.Thread):
def run(self):
print "Master Started"
child.start()
while child.isAlive():
time.sleep(0.1)
print "Master Finished"

master = masterThread()
master.start()

---------------SCRIPT OUTPUT--------------

Master Started
1
2
3
4
5
6
7
8
9
10
Master Finished

Hope this helps.

Christian
http://www.dowski.com
 
G

Giovanni Tumiati

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.
 
P

Prashanth Ellina

Hi,

Thanks for the code sample. I will try it out. I guess there is no
reliable way to get away with just the "threads" module.

Thanks,
Prashanth Ellina
 
P

Peter Hansen

Prashanth said:
Thanks for the code sample. I will try it out. I guess there is no
reliable way to get away with just the "threads" module.

As "threading" is built on top of "thread", that statement seems wrong,
but the real question you should ask yourself is why you want to use the
"thread" module at all when "threading" already exists.

Also, I have to say I'm confused by Giovanni's "solution", as it seems
to do nothing that isn't already provided by threading.Thread's
isAlive() and join() methods (which I already mentioned in my previous
posting).

-Peter
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top