Alex Rivera | Logout

Python: signal.pause() equivalent on Windows

Asked 2012-03-20T10:26:19.130
13

I have my main application thread that spawns 2 threads and I catch SIGINT in my main thread to quit them nicely. On linux, I'm using signal.pause() and it works perfectly.

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

My ugly solution is:

my_queue.get(True, averylongtime)

And put something in my_queue in my signal handler. Note that if I don't specify a timeout, SIGINT is not caught. But I wonder if there's a better solution.

Thank you

Edit
Report

1 Answer

0

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 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".

answered 2012-03-23T12:40:08.227

Your Answer