473,324 Members | 2,511 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

How do I know when a thread quits?

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

Jul 19 '05 #1
6 15024
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 -

--
Outside of a dog, a book is a man's best friend: and inside a
dog, it's too dark to read.
-- Groucho Marx

Jul 19 '05 #2
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
Jul 19 '05 #3
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

Jul 19 '05 #4
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.

Jul 19 '05 #5
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

Jul 19 '05 #6
Prashanth Ellina wrote:
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
Jul 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Valkyrie | last post by:
Refering to the following codes I found, there is nothing displayed in the console, may I ask why? def thrd(param): # the thread worker function print "Received",param import thread for i in...
0
by: Greg Bacon | last post by:
We have a messaging application that makes seemingly unremarkable use of a HelpProvider component. However, after we've received a message, if we display the help popup (either via F1 or through...
9
by: Joakim Braun | last post by:
Unonload doesn't get much time when quitting the browser. So how can you do time-consuming actions then? I'm doing a web-based MySQL interface. Records are presented as tables of links. The...
5
by: code_wrong | last post by:
Hi Firefox does not support the IE specific 'event' identifier and just quits running the script. ..... even though the identifier only turns up in code marked for IE only ... Does this seem...
4
by: sherifffruitfly | last post by:
Hi, I load the contents of an excel sheet into a dataset via the excel object library (couldn't get the #$%#$^ oledb/ado route to work - #^$%&^%$ connection strings... grrr.... ) Anyway,...
10
by: =?Utf-8?B?UHVjY2E=?= | last post by:
Hi, I'm using vs2005 and .net 2.0. I started 2 threadpool threads. How do I know when they're done with their tasks? Thanks. ThreadPool.QueueUserWorkItem(new...
7
by: mehdi | last post by:
Hi folks, Consider a class (MyClass) in which one of its methods (named Run) is supposed to run in another thread, rather than the Main thread. This class uses the System.Threading.Thread class to...
10
by: ohmmega | last post by:
hello out there, I use a thread data from a linuxserver. i ask for data, i read the data (fix in size) - so far so good. this works great for the first 61 datapackages, then my thread abruptly...
3
by: Saser | last post by:
Hi all bytes out there. I have a few little problems in my program. The program is supposed to help me to not spend too much time of my day at the computer ;) I play a game called...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.