Re: [Tutor] Waiting until a thread ends

2011-03-19 Thread lists
>
>
>
>> In my continuing quest the find the best way of doing this I came across
>> the following method:
>>
>> for thread in threading.enumerate():
>> if thread is not threading.currentThread():
>> thread.join()
>> print 'FINISHED'
>>
>> In my newbie understanding, you can't join() the current thread, because
>> it's the main thread (the one from which the others are called), join()ing
>> it would lock the program up (it would never complete).
>>
>> The above only join()s a thread if it isn't the current thread, thus
>> (hopefully) getting around this. Swapping my earlier stupid code for this
>> seems to work as expected in my tests.
>>
>
> Thanks for the postbacks, it's been useful/interesting for me.
>
> Best,
>
> Walter
>

I'm really pleased that it was of some help to somebody else too.

Kind Regards,

Chris
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Waiting until a thread ends

2011-03-19 Thread lists
>
>
>> Only really glanced at this, but you seem to be checking only the last
>>> thread *after* the loop?  Surely you should be storing all the threads in a
>>> list (or someplace) as you create them, and then check them all for liveness
>>> and if so join them each in turn, to ensure you only print 'FINISHED' once
>>> you've checked and confirmed that all the threads created have in fact
>>> finished.
>>>
>>> Walter
>>>
>>>
>> That makes absolute sense. Doh on my part!
>>
>> Thanks!
>>
>>
> Just done a little more reading and came across this in an O'Reilly article
> here http://www.oreillynet.com/onlamp/blog/2008/01/pymotw_threading.html
>
> Seems like an elegant way to accomplish a wait until all running threads
> have finished.
>
> Using enumerate() to wait for all running threads:
>
> It is not necessary to retain an explicit handle to all of the daemon
> threads you start in order to ensure they have completed before exiting the
> main process. threading.enumerate()returns a list of active Thread instances.
> The list includes the current thread, and since joining the current thread
> is not allowed (it introduces a deadlock situation), we must check before
> joining.
>
>
In my continuing quest the find the best way of doing this I came across the
following method:

for thread in threading.enumerate():
if thread is not threading.currentThread():
thread.join()
print 'FINISHED'

In my newbie understanding, you can't join() the current thread, because
it's the main thread (the one from which the others are called), join()ing
it would lock the program up (it would never complete).

The above only join()s a thread if it isn't the current thread, thus
(hopefully) getting around this. Swapping my earlier stupid code for this
seems to work as expected in my tests.

Chris
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Waiting until a thread ends

2011-03-17 Thread lists
> Only really glanced at this, but you seem to be checking only the last
>> thread *after* the loop?  Surely you should be storing all the threads in a
>> list (or someplace) as you create them, and then check them all for liveness
>> and if so join them each in turn, to ensure you only print 'FINISHED' once
>> you've checked and confirmed that all the threads created have in fact
>> finished.
>>
>> Walter
>>
>>
> That makes absolute sense. Doh on my part!
>
> Thanks!
>
>
Just done a little more reading and came across this in an O'Reilly article
here http://www.oreillynet.com/onlamp/blog/2008/01/pymotw_threading.html

Seems like an elegant way to accomplish a wait until all running threads
have finished.

Using enumerate() to wait for all running threads:

It is not necessary to retain an explicit handle to all of the daemon
threads you start in order to ensure they have completed before exiting the
main process. threading.enumerate()returns a list of active Thread instances.
The list includes the current thread, and since joining the current thread
is not allowed (it introduces a deadlock situation), we must check before
joining.


import randomimport threadingimport time
def worker():
"""thread worker function"""
t = threading.currentThread()
pause = random.randint(1,5)
print 'Starting:', t.getName(), 'sleeping', pause
time.sleep(pause)
print 'Ending  :', t.getName()
return
for i in range(3):
t = threading.Thread(target=worker)
t.setDaemon(True)
t.start()
main_thread = threading.currentThread()for t in threading.enumerate():
if t is main_thread:
continue
print 'Joining :', t.getName()
t.join()
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Waiting until a thread ends

2011-03-17 Thread lists
>
> Only really glanced at this, but you seem to be checking only the last
> thread *after* the loop?  Surely you should be storing all the threads in a
> list (or someplace) as you create them, and then check them all for liveness
> and if so join them each in turn, to ensure you only print 'FINISHED' once
> you've checked and confirmed that all the threads created have in fact
> finished.
>
> Walter
>
>
That makes absolute sense. Doh on my part!

Thanks!
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor