Ctrl-C does not work when using threading.Timer

I am writing a multi-threaded Python application on Windows.

I used to end the application using ctrl-c , but as soon as I added that threading.Timer instances of ctrl-c stopped working (or sometimes it takes a lot of time).

How could this be? What is the relationship between timer threads and ctrl-c ?

UPDATE:
I found the following in Python threading documentation :

Streams interact strangely with interruption: KeyboardInterrupt exception will be an arbitrary thread. (When a signal module is available, interrupts always go to the main topic.)

+6
source share
2 answers

The way threading.Thread (and therefore threading.Timer ) is that each thread is registered with the threading module, and after the interpreter exits, the interpreter will wait until all registered threads are completed before the interpreter itself completes. This is done so that the threads actually finish execution, instead of the interpreter being brutally removed from under them. Therefore, when you press ^ C, the main thread receives a signal, decides to end, and waits for the timer to end.

You can set daemonic streams (using the setDaemon method) so that the streaming module does not wait for these streams, but if they execute Python code when the interpreter exits, you get confused errors when exiting. Even if you cancel threading.Timer (and set it with a daemon), it can still wake up during the destruction of the interpreter, because the threading.Timer cancel method simply tells threading.Timer do nothing when it wakes up, but it should actually execute the code Python to make this determination.

There is no elegant way to terminate threads (other than the current one) and a reliable way to terminate a thread that is blocked. A more manageable approach to timers is usually an event loop, such as graphical interfaces and other event-driven systems. What to use depends entirely on what else your program will do.

+4
source

There is a presentation by David Basley that sheds some light on the subject. PDF is available here . Look at pages 22-25 (“Interlude: Signals” on “Frozen Signals”).

+2
source

Source: https://habr.com/ru/post/892114/


All Articles