Python: signal.pause() equivalent on Windows Python: signal.pause() equivalent on Windows windows windows

Python: signal.pause() equivalent on Windows


I use this:

#another:while not self.quit:    # your code# maintry:    # your codeexcept KeyboardInterrupt:    another.quit = True    time.sleep(5) # or wait for threading.enumerate() or similar

If I want it more robust, say, exit in presence of bugs too:

except KeyboardInterrupt:    another.quit = True    signal.alarm(5)    time.sleep(6)

A side effect to this is that every block where you except: or except Exception, e: (which is not something you should do anyway/much) you have to prepend except KeyboardInterrupt: raise so that the exception is not "eaten".


I use this for catching a ctrl-c on windows. In case I'm writing to a pipe or file or what have you.. I want to exit gracefully. Below is a toy example

import signalimport sysdef signal_handler(signal, frame):        print('Process Interrupted!\n\a')    sys.exit(0)signal.signal(signal.SIGINT,signal_handler)#Rest of your code