If I have a thread in an infinite loop, is there a way to terminate it when the main program ends (for example, when I press Ctrl+C)?
Check this question. The correct answer has great explanation on how to terminate threads the right way: Is there any way to kill a Thread in Python?
To make the thread stop on Keyboard Interrupt signal (ctrl+c) you can catch the exception "KeyboardInterrupt" and cleanup before exiting. Like this:
try:
start_thread()
except (KeyboardInterrupt, SystemExit):
cleanup_stop_thread()
sys.exit()
This way you can control what to do whenever the program is abruptly terminated.
You can also use the built-in signal module that lets you setup signal handlers (in your specific case the SIGINT signal): http://docs.python.org/library/signal.html
cleanup_stop_thread()
a global function that I can use? or should I need to implement it? –
Igneous If you make your worker threads daemon threads, they will die when all your non-daemon threads (e.g. the main thread) have exited.
http://docs.python.org/library/threading.html#threading.Thread.daemon
isDaemon()
is False, set it True by setDaemon(True)
. –
Headed isDaemon()
and setDaemon()
are old getters/setters (as per the linked doc above), just use daemon=True
in threading.Thread()
–
Baroda Check this question. The correct answer has great explanation on how to terminate threads the right way: Is there any way to kill a Thread in Python?
To make the thread stop on Keyboard Interrupt signal (ctrl+c) you can catch the exception "KeyboardInterrupt" and cleanup before exiting. Like this:
try:
start_thread()
except (KeyboardInterrupt, SystemExit):
cleanup_stop_thread()
sys.exit()
This way you can control what to do whenever the program is abruptly terminated.
You can also use the built-in signal module that lets you setup signal handlers (in your specific case the SIGINT signal): http://docs.python.org/library/signal.html
cleanup_stop_thread()
a global function that I can use? or should I need to implement it? –
Igneous Try to enable the sub-thread as daemon-thread.
Recommended:
from threading import Thread
t = Thread(target=desired_method)
t.daemon = True # Dies when main thread (only non-daemon thread) exits.
t.start()
Inline:
t = Thread(target=desired_method, daemon=True).start()
Old API:
t.setDaemon(True)
t.start()
When your main thread terminates (e.g. Ctrl+C keystrokes), other threads will also be killed by the instructions above.
Use the atexit module of Python's standard library to register "termination" functions that get called (on the main thread) on any reasonably "clean" termination of the main thread, including an uncaught exception such as KeyboardInterrupt
. Such termination functions may (though inevitably in the main thread!) call any stop
function you require; together with the possibility of setting a thread as daemon
, that gives you the tools to properly design the system functionality you need.
atexit.register()
calls in Python modules, causing your termination procedure to be executed after the multiprocessing
's one. I run in this problem dealing with Queue
and daemon
threads: “EOF error” at program exit using multiprocessing Queue and Thread. –
Transom atexit
handlers aren't called when non-daemon threads are alive and the main thread exits. See Script stuck on exit when using atexit to terminate threads. –
Sympathy If you spawn a Thread like so - myThread = Thread(target = function)
- and then do myThread.start(); myThread.join()
. When CTRL-C is initiated, the main thread doesn't exit because it is waiting on that blocking myThread.join()
call. To fix this, simply put in a timeout on the .join() call. The timeout can be as long as you wish. If you want it to wait indefinitely, just put in a really long timeout, like 99999. It's also good practice to do myThread.daemon = True
so all the threads exit when the main thread(non-daemon) exits.
myThread.daemon = True
is a wonderful solution to this problem. –
Budge .daemon=True
is in not a solid solution. Check this thread for an explanation: https://mcmap.net/q/212096/-python-exception-in-thread-thread-1-most-likely-raised-during-interpreter-shutdown –
Fillender Daemon threads are killed ungracefully so any finalizer instructions are not executed. A possible solution is to check is main thread is alive instead of infinite loop.
E.g. for Python 3:
while threading.main_thread().isAlive():
do.you.subthread.thing()
gracefully.close.the.thread()
See Check if the Main Thread is still alive from another thread.
© 2022 - 2024 — McMap. All rights reserved.