You can just set the daemon
flag on the Thread
objects to True
prior to starting the threads. As stated in the documentation:
A thread can be flagged as a “daemon thread”. The significance of this
flag is that the entire Python program exits when only daemon threads
are left. The initial value is inherited from the creating thread. The
flag can be set through the daemon
property.
It's also worth noting that this is potentially dangerous, depending on what your background threads are doing (again, quoting the documentation):
Daemon threads are abruptly stopped at shutdown. Their resources (such
as open files, database transactions, etc.) may not be released
properly. If you want your threads to stop gracefully, make them
non-daemonic and use a suitable signalling mechanism such as an Event
.
The way you implement a graceful shutdown is somewhat dependent on how your threaded code is implemented, but it would look something like this:
Thread method:
def threaded_method(event, ...):
while True:
if event.is_set()
# clean up
return
# Normal stuff it does goes here.
Main thread:
def on_exit(): # called when X in main window pressed
event.set() # Tell threads to cleanup.
# Do normal exit stuff.
if __name__ == "__main__":
event = threading.Event()
t = threading.Thread(target=threaded_method, args=(event, ...))
#t.daemon = True # Assuming you don't care about gracefully stopping the threads, you can just do this and leave out the Event stuff.
t.start()
# other stuff
Note that it's possible that this approach may not work, if the loop in your thread is blocking on something (like getting data from a sub-process) that doesn't unblock when your program exits. Let me know if that's the case and I can probably rework my answer to cover that.