Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to stop time.sleep in windows?

Tags:

python

windows

In *nix python signaling allows me to stop sleep before it is ready. Is there any similar mechanism available in windows -- seems that all the methods end up intercepting code only after the sleep?

Code example:

from time import sleep
.. do something that will intercept the sleep
try:
    sleep(60)
finally:
    print 'how to get here under 60 seconds in windows?'

Similar question that doesn't have an answer for windows: break/interrupt a time.sleep() in python

like image 593
mkorpela Avatar asked Dec 08 '11 12:12

mkorpela


2 Answers

The Python documentation for signal says:

• Although Python signal handlers are called asynchronously as far as the Python user is concerned, they can only occur between the “atomic” instructions of the Python interpreter. This means that signals arriving during long calculations implemented purely in C (such as regular expression matches on large bodies of text) may be delayed for an arbitrary amount of time.

whereas time says:

The actual suspension time may be less than that requested because any caught signal will terminate the sleep() following execution of that signal’s catching routine.

On Windows, apparently time.sleep() is not implemented in accordance with the documentation, because the handler for a signal received during sleep is not acted upon until the full duration of the sleep is finished. The following example:

import signal, time

def handler(signum, frame):
    print('Signal handler called with signal', signum)

signal.signal(signal.SIGINT, handler)

print("signal set, sleeping")
time.sleep(10)
print("sleep done")

prints:

signal set, sleeping
Signal handler called with signal 2
sleep done

with the first line occurring immediately and the second two after 10 seconds, regardless of when the interrupt occurs.

As Thomas K suggests, a better strategy would be to use threads and synchronization.

like image 84
Dave Avatar answered Sep 20 '22 18:09

Dave


I'm not sure if this will do it for you, but here's a little workaround you could do:

for i in range(60):
    if checkIfIShouldInterruptSleep():
         break
    time.sleep(1)
print "Finished Sleeping!"

It's only accurate to the second, but it might serve your purpose. Happy coding!

like image 44
Cosine Avatar answered Sep 18 '22 18:09

Cosine