I could not interrupt my threaded Python production app using Ctrl+C on Windows, it continues running, tried both exception and signal handling. Here is very simplified version of code, that does not interrupts. Single thread app terminates fine, same as multithreaded Linux version. Could anybody help with this trouble? Thanks in advance.
import threading
import time
class FooThread(threading.Thread):
stop_flag = False
def __init__(self):
threading.Thread.__init__(self)
def run(self):
while not self.stop_flag:
print(1)
time.sleep(1)
t = FooThread()
t.start()
try:
t.join()
except KeyboardInterrupt:
t.stop_flag = True
t.join()
joinmethod. Internally, the wait could be modified in two ways to allow interruption in the main thread. Either switch to usingWaitForMultipleObjectsand include an Event that gets set by theSIGINThandler (which gets called by the C runtime's console control handler) or switch to using an alertableWaitForSingleObjectExwait and have the SIGINT handler queue an APC to alert the main thread.SleepConditionVariableSRWandWakeConditionVariable. Whatever it takes will likely involve a major rewrite.