Is there a way one can interrupt the sleep() function with a key press in Python?

Look at KeyboardInterrupt in the Python reference. Right now, I don't have a good example. I try to work on one!

So far I have only found one function that will work during the active sleep() ...

import time
import msvcrt
# prints the key value during sleep
if msvcrt.kbhit():
    print msvcrt.getch()

Ouch! The IDE fooled me! This does not make sense, forget it! Sorry!

If it's anything like the sleep(t) function I have been using with C++, there is no way to stop it early. Maybe ctrl/alt/del, but that is not what you want to normally do.

havn't got an example, but my very rough guess is maybe you could do something along the lines of;

wrap a function around the time.sleep() function that when called it starts the sleep in another thread so that the program will only continue untill;
sleep ends,
thread gets killed (stoppped)
so the way to interrupt could be to manually shut down the thread which in turn would shut down the sleep


you could make your own sleep class that allows you to kill it?

something along those lines anyway
not too experienced on threading or using classes

Thanks a1eio,
I just remembered hanging an old Delphi snippet on the DaniWeb code snippet laundry line a long time ago, that talked about the very same thing that Ene brought up.

no probs, glad i could help :)

*edit: that smiley looks evil

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.