How do I make Python listen for external input while running? I don't mean "raw_input()", I mean where the program will be running happily, and then if I press "q" while its running, it quits... Or something along those lines...
How do I make Python listen for external input while running? I don't mean "raw_input()", I mean where the program will be running happily, and then if I press "q" while its running, it quits... Or something along those lines...
simplest method is to catch the KeyboardInterrupt exception that is generated when the user presses ctrl+C
otherwise you may need to look at a terminal handling library like curses.Code:try: code_that_takes_long_time() except KeyboardInterrupt: print "stopped" clean_up()
you can use threads.
Bookmarks