Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Threading question

Reply
Thread Tools

Threading question

 
 
Torsten Marek
Guest
Posts: n/a
 
      04-15-2004
Hello to all,

I have a simple question about threads in Python. I am starting a
number of threads in my program and I want print out the amount of time
needed after all threads stopped. Do I need to explictly join the
threads, set a condition or is it possible to somehow work with
try/finally like that:

try:
for i in range(0, num_threads):
s = MyThread()
s.start()
finally:
print "all threads finished"


Thanks in advance

Torsten
 
Reply With Quote
 
 
 
 
Peter Hansen
Guest
Posts: n/a
 
      04-15-2004
Torsten Marek wrote:

> I have a simple question about threads in Python. I am starting a
> number of threads in my program and I want print out the amount of time
> needed after all threads stopped. Do I need to explictly join the
> threads, set a condition or is it possible to somehow work with
> try/finally like that:
>
> try:
> for i in range(0, num_threads):
> s = MyThread()
> s.start()
> finally:
> print "all threads finished"


The other threads are running in, well, other threads, so you
won't see any effect on the above thread when they end. Therefore
you can't get much mileage out of a finally clause. A .join()
is definitely the way to go here.

-Peter
 
Reply With Quote
 
 
 
 
Torsten Marek
Guest
Posts: n/a
 
      04-15-2004
Peter Hansen schrieb:
> Torsten Marek wrote:
>
>> I have a simple question about threads in Python. I am starting a
>> number of threads in my program and I want print out the amount of
>> time needed after all threads stopped. Do I need to explictly join the
>> threads, set a condition or is it possible to somehow work with
>> try/finally like that:
>>
>> try:
>> for i in range(0, num_threads):
>> s = MyThread()
>> s.start()
>> finally:
>> print "all threads finished"

>
>
> The other threads are running in, well, other threads, so you
> won't see any effect on the above thread when they end. Therefore
> you can't get much mileage out of a finally clause. A .join()
> is definitely the way to go here.
>
> -Peter

I found another way to go for now, which works fine for me.
Since the threading module sets sys.exitfunc, I just do:

def my_exitfunc():
global thread_wait
thread_wait()
# own stuff follows here...

thread_wait = sys.exitfunc
sys.exitfunc = my_exitfunc

That's maybe not very clean, but it's just a minor script I wrote, so I
don't want to waste to much time on it

Anyway, thanks for the answer

Torsten
 
Reply With Quote
 
Peter Hansen
Guest
Posts: n/a
 
      04-15-2004
Torsten Marek wrote:

> Peter Hansen schrieb:
>> The other threads are running in, well, other threads, so you
>> won't see any effect on the above thread when they end. Therefore
>> you can't get much mileage out of a finally clause. A .join()
>> is definitely the way to go here.


> I found another way to go for now, which works fine for me.
> Since the threading module sets sys.exitfunc, I just do:
>
> def my_exitfunc():
> global thread_wait
> thread_wait()
> # own stuff follows here...
>
> thread_wait = sys.exitfunc
> sys.exitfunc = my_exitfunc
>
> That's maybe not very clean, but it's just a minor script I wrote, so I
> don't want to waste to much time on it


Good solution. You're actually taking advantage of the fact that
the main thread does a join() on all non-daemon threads automatically
as it exits. The source in threading.py for the "main thread"
can make illuminating reading...

-Peter
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: threading in PyQt vs threading in standard library Steven Woody Python 0 01-09-2009 07:48 AM
threading in PyQt vs threading in standard library Steven Woody Python 0 01-09-2009 07:15 AM
Cooperative threading preemptive threading - a bit confused failure_to@yahoo.co.uk Java 9 12-29-2007 01:10 AM
Threading question Frederick Wilson Firefox 1 12-22-2004 12:23 AM
disconnected DataSet multi-threading question Alina ASP .Net 0 07-16-2003 04:23 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57