Hi, I'm writing a command line program that watches a file, and recompiles it when it changes. However, there should also be a possibility of doing a complete clean restart (cleaning up temp files, compiling some dependencies, etc.).
Since the program is in an infinite loop, there are limited means of interacting with it. Right now, I'm using the Keyboard Interrupt: if the user presses CTRL+C once, a clean restart is done, if he presses it twice within a second, the program terminates. The stripped down code looks like this: while True: try: time.sleep(1) if watched_file_has_changed(): compile_the_changed_file() except KeyboardInterrupt: # user hits CTRL+C try: print("Hit Ctrl+C again to quit") time.sleep(1) clean_restart() except KeyboardInterrupt: do_some_cleanup() sys.exit(0) Is there another way of doing this? Ideally, there would be an exception every time any key at all is pressed while the code in the try block is being executed. That way, the user could just hit the 'R' key to do a clean restart, and the 'E' key to exit the program. Is there any way to implement something like that? Right now, the CTRL+C solution works, but isn't very extensible (It wouldn't be easy to add another command, for example). Any suggestions? Thanks, Michael -- http://mail.python.org/mailman/listinfo/python-list