By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,098 Members | 1,881 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,098 IT Pros & Developers. It's quick & easy.

How to stop program when threads is sleeping

P: n/a
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.

I also thought to put 'time.sleep()' to the main thread. But I
think the main thread will not response to user's action because it is
executing sleep().

Any ideas?
Thanks.

Dec 25 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a

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.

I also thought to put 'time.sleep()' to the main thread. But I
think the main thread will not response to user's action because it is
executing sleep().

Any ideas?
Thanks.
The thread should not "sleep" - just take short "cat naps". The main
should then set some kind of counter, and the thread check the counter
between naps...

Dec 25 '06 #2

P: n/a
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.
Dec 25 '06 #3

P: n/a

Carsten Haese wrote:
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.
While , there is something wrong in my expression. What I mean is the
thread will wait some time after doing some tasks. I want to know is
there any method to end the thread or make it out of execution of
waiting. I use time.sleep() to let the thread wait.
Thanks.

Dec 26 '06 #4

P: n/a

many_years_after wrote:
Carsten Haese wrote:
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.

While , there is something wrong in my expression. What I mean is the
thread will wait some time after doing some tasks. I want to know is
there any method to end the thread or make it out of execution of
waiting. I use time.sleep() to let the thread wait.
So you want a way of stopping a thread while its blocked (waiting) what
ever seconds in the time.sleep() method ?
Thanks.
If the thread is supposed to do more operation then i would follow what
Carsten suggested.

Cheers

Dec 26 '06 #5

P: n/a
On 25 Dec 2006 21:50:15 -0800, many_years_after wrote
While , there is something wrong in my expression. What I mean is the
thread will wait some time after doing some tasks. I want to know is
there any method to end the thread or make it out of execution of
waiting. I use time.sleep() to let the thread wait.
We can't help you if you don't tell us what you need. It's still not quite
clear what exactly you need, but one thing you should know is that you can't
stop a thread while it's sleep()ing, so using sleep() is probably the wrong
approach.

The wait() method of the threading.Event object from my previous response can
take an optional timeout argument. This will make your thread wait until the
timeout elapses or until the event is .set() by another thread, whichever
comes first.

Hope this helps. If this doesn't help, please tell us exactly what you need
and maybe show us some code.

Carsten.

Dec 26 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.