I want to kill a running program by hitting ctrl-c but it uses threads. I *know* threads are not killable but there must be a way to do this.
Here's my setup: a telnet server runs, accepting logging, processing user input, etc. It's a MOO, in fact. But I'm trying to implement timed events and I don't see another way to do this than by using threads. While the server handles connections, a thread checks every second if something has to be done (like kicking out idle players, handling combat or specific timed actions, etc.)
The whole thing is a console server, ctrl-c is used to shutdown the server. I tried trapping KeyboardInterrupt but it didn't work:
The previous example can only be shutdown by manually killing the python process. Ugly.
self.stopevent = Event()
while not self.stopevent.isSet():
The following version doesn't work either. It stops on ctrl-c but the thread doesn't even start.
Instead of having the thread pool a certain variable and if it's set terminate itself, is it possible to trap the thread to stop on ctrl-c? I know that python forces you to cleanly terminate your threads but I need to be able to simply shutdown the server with a simple command.
self._stopevent = Event()
while not KeyboardInterrupt: