Python: how to terminate a thread when the main program terminates

If I have a thread in an infinite loop, is there a way to stop it when the main program ends (for example, when I press Ctrl + C )?

+72
python multithreading python-multithreading
Apr 01 '10 at 22:44
source share
6 answers

Check out this question. The correct answer has a great explanation on how to properly complete threads: Is there a way to kill a thread in Python?

To stop the flow by keyboard interrupt (ctrl + c), you can catch the “KeyboardInterrupt” exception and clear it before exiting. Like this:

try: start_thread() except (KeyboardInterrupt, SystemExit): cleanup_stop_thread() sys.exit() 

This way you can control what to do when the program suddenly terminates.

You can also use the built-in signal module, which allows you to configure signal handlers (in your particular case, a SIGINT signal): http://docs.python.org/library/signal.html

+42
Apr 01 '10 at
source share

If you create workflow threads for workflows, they will die when all of your non-daemon threads (such as the main thread) exit.

http://docs.python.org/library/threading.html#threading.Thread.daemon

+83
Apr 01 '10 at 23:35
source share

Use the atexit module of the Python standard library to register "completion" functions that are called (on the main thread) at any reasonable "clean" termination of the main thread, including an uncaught exception, such as KeyboardInterrupt . Such termination functions can (though inevitably in the main thread!) Call any required stop function; along with the ability to install a stream like daemon , which gives you the tools to properly design the system functions you need.

+12
Apr 02 '10 at 2:43
source share

If you create Thread the same way - myThread = Thread(target = function) - and then do myThread.start(); myThread.join() myThread.start(); myThread.join() . When CTRL-C is triggered, the main thread does not exit because it is waiting to block the call to myThread.join() . To fix this, simply set a timeout on the .join () call. The timeout can be as long as you want it. If you want it to wait indefinitely, just enter a very long timeout, for example 99999. It is also good to make myThread.daemon = True so that all threads exit when the main thread (non-daemon) exits.

+8
Aug 13 '13 at 21:25
source share

Try turning on the subflow as daemon-thread.

For example:

 from threading import Thread threaded = Thread(target=<your-method>) threaded.daemon = True # This thread dies when main thread (only non-daemon thread) exits. threaded.start() 

Or (in line):

 from threading import Thread threaded = Thread(target=<your-method>, daemon=True).start() 



When your main thread terminates ("for example, when I press Ctrl + C "), other threads are killed using the above instruction.

+8
Aug 07 '18 at 9:28
source share

Demon threads are gracefully destroyed, so any finalizer instructions are not followed. A possible solution is to check if the main thread is a living, not an infinite loop.

For example. for Python 3:

 while threading.main_thread().isAlive(): do.you.subthread.thing() gracefully.close.the.thread() 

See . Check if the main thread is alive in another thread .

0
Aug 21 '19 at 6:08
source share



All Articles