Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

break/interrupt a time.sleep() in python

Tags:

python

I need to break from time.sleep() using ctrl c.

While 1:     time.sleep(60) 

In the above code when the control enters time.sleep function an entire 60 seconds needs to elapsed for python to handled the CTRL C

Is there any elegant way to do it. such that I can interrupt even when the control is in time.sleep function

edit

I was testing it on a legacy implementation which uses python 2.2 on windows 2000 which caused all the trouble . If I had used a higher version of python CTRL C would have interrupted the sleep() . I did a quick hack by calling sleep(1) inside a for loop . which temporarily fixed my issue

like image 594
Anuj Avatar asked Feb 25 '11 06:02

Anuj


People also ask

How do you break sleep time in Python?

In most systems, Ctrl-C would interrupt the sleep. It certainly does on Unix and on Mac. Which system are you on? – DS.

What is sleep () method in Python?

Python sleep() The sleep() function suspends (waits) execution of the current thread for a given number of seconds. Python has a module named time which provides several useful functions to handle time-related tasks.

How do you wait 0.5 seconds in Python?

If you've got a Python program and you want to make it wait, you can use a simple function like this one: time. sleep(x) where x is the number of seconds that you want your program to wait.


2 Answers

The correct answer is to use python stdlib's threading.Event

Sure you can tune down your sleep interval so you sleep for very short periods, but what if you actually want to run your loop once every 60s? Then you need to do more work to determine if it's time to run or just keep sleeping. Furthermore, you're still technically blocking but for only a short period of time. Contrast to threading.Event:

from threading import Event  exit = Event()  def main():     while not exit.is_set():       do_my_thing()       exit.wait(60)      print("All done!")     # perform any cleanup here  def quit(signo, _frame):     print("Interrupted by %d, shutting down" % signo)     exit.set()  if __name__ == '__main__':      import signal     for sig in ('TERM', 'HUP', 'INT'):         signal.signal(getattr(signal, 'SIG'+sig), quit);      main() 

When the signal handler calls exit.set(), the main thread's wait() call will immediately be interrupted.

Now, you could use an Event to signal that there's more work to do, etc. But in this case it does double duty as a convenient indicator that we want to quit (e.g. the while not exit.is_set() part.)

You also have the option to put any cleanup code after your while loop.

like image 110
thom_nic Avatar answered Sep 23 '22 07:09

thom_nic


Not sure what the sense of this code is - but if necessary use a shorter sleep() interval and put a for loop around it:

for i in range(60):    sleep(1) 

Catching the KeyboardInterrupt exception using try..except is straight-forward

like image 43
Andreas Jung Avatar answered Sep 22 '22 07:09

Andreas Jung