Anuj Anuj -4 years ago 659
Python Question

break/interrupt a time.sleep() in python

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

While 1:

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


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

Answer Source

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

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download