Python: signal.pause () equivalent on Windows

I have my main application thread that spawns 2 threads and I will catch SIGINT in my main thread to terminate them. On linux, I use signal.pause() and it works fine.

What is the best way to implement signal.pause () on Windows?

My ugly solution:

 my_queue.get(True, averylongtime) 

And put something in my_queue in the signal handler. Please note: if I do not specify a timeout, SIGINT will not get caught. But I wonder if there is a better solution.

thanks

+7
source share
2 answers

I use this:

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

If I want it to be more reliable, let's say we go out and have errors:

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

A side effect for this is that every block where you are except: or except Exception, e: (which is not something that you should do anyway / a lot), you need to add except KeyboardInterrupt: raise so that the exception does not was "eaten".

0
source

I use this to search for ctrl-c on windows. In case I write to a pipe or file or what you have. I want to go out gracefully. Below is an example of a toy

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

All Articles