473,418 Members | 2,029 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,418 software developers and data experts.

How to stop program when threads is sleeping

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
5 6440

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
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

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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: shyren | last post by:
Hi All, I have a php program which calls a c++ program using exec. However when user presses stop button in the browser or closes it this program keeps on running on the server. How can I stop it...
2
by: Dave Reed | last post by:
I seem to remeber reading somewhere there was a statement you could put in your python program to stop its execution and start the interactive interpreter at that point (and then you could change...
2
by: ajikoe | last post by:
Hello, How can we stop this peace of code in KOMODO: while 1: print "helo" I can't stop it, even using Ctrl+C pujo
2
by: trint | last post by:
Ok, I click on 'exit' and have it calling (function is started in new thread): this.m_notifyicon.Dispose(); Application.Exit(); or this.Close(); And TaskManager shows it still running!
3
by: prakashsha | last post by:
HI, I've an asp.net page which has a button. On click of the button the program queries the database and gets the result and displays the same.Now assume that my program takes 2 minutes to get the...
1
by: Vaclav Havlik | last post by:
Hello. Could you advise me about this, please? I write a multi-thread program using the 'threading' module. Moreover, I use C++ extension. Two threads work inside this C++ extension in such way that...
1
by: kazpsp | last post by:
i want my program that when it ends instead of closing it goes back to the top like an example: main() { int go; cout<<"text here "; cin>>go; if(go==10)
1
by: CL4life | last post by:
I have the following problem: a program, located on drive a known drive (for example lets say drive C:) needs to execute a program on an unknown different drive (a USB device which doesn't have the...
3
by: GiJeet | last post by:
I put the the button property in the watch window but it doesn't stop when the property changes value. For example I put this.myButton.Visible in the watch window with an initial value of false...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.