How to stop program when threads is sleeping

Carsten Haese carsten at uniqsys.com
Mon Dec 25 11:22:55 EST 2006


On Sun, 2006-12-24 at 22:55 -0800, many_years_after wrote:
> Hi, pythoners:
> 
>       There is a problem I couldn't dispose. I start a thread in the my
> program. The thread will do something before executing time.sleep().
> When the user give a signal to the main thread (such as click the 'end'
> button or close the window), the thread should end it's running. But
> how to end the threading when it's sleeping? I set an flag to the
> thread, but it doesn't work.

Is the thread supposed to do some additional work after being woken up?
If not, there is no point in going to sleep in the first place and the
thread should just terminate when it has completed its task. If yes, I'd
use a threading.Event object to .wait() on in the sub-thread rather than
putting it to sleep, and then .set() the event object in the main thread
when it's time to wake up the sub-thread.

Hope this helps,

Carsten.





More information about the Python-list mailing list