0

I want to know: what is the best practice for killing threads started by a main Python application in the case the main application receives a SIGINT?

I am doing the following thing, but I HIGHLY suspect that because needing to kill other started threads is such a common problem, that probably there is a better way to do it:

class Handler(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        self.keep_go = True

    def run(self):
        while self.keep_go:
            #do something

    def stop(self): #seems like i shouldn't have to do this myself
        self.keep_go = False
try:
    h = Handler()
    h.start()
    while True:   #ENTER SOME OTHER LOOP HERE
        #do something else
except KeyboardInterrupt: #seems like i shouldn't have to do this myself
    pass
finally:
    h.stop()

The following post is related, but it is not clear to me what the actual recommended practice is, because the answers are more of a "here's some possibly hackish way you can do this". Also, I do not need to kill somethng "abruptly"; I am ok with doing it "the right way": Is there any way to kill a Thread in Python?

Edit: I guess one minor flaw with my approach is that it does not kill the current processing in the while loop. It does not receive a "kill event" that "rolls" back this loop as a transaction, nor does it halt the remainder of the loop.

Community
  • 1
  • 1
Tommy
  • 12,588
  • 14
  • 59
  • 110
  • It looks like you made a mistake copying in your example code; the `try` is missing. – dano Nov 20 '15 at 19:03
  • The code you pasted above doesn't do what you think it will do: `h.start()` doesn't block, so `h.stop()` is immediately called afterwards, causing the thread to stop, and the whole program to exit. – dano Nov 20 '15 at 19:08
  • I'm no expert, but this is how I've accomplished closing threads, as well. If you have a thread whose function is to continuously run (e.g. to monitor the status of something), how else will it know to stop without having some way to tell it to do so, such as what you currently do? – Dval Nov 20 '15 at 19:10
  • @dano yes this is true, another copy paste error made when trying to generalize my problem – Tommy Nov 20 '15 at 19:11

1 Answers1

2

I usually just set each thread's daemon attribute to True. That way, when the main thread terminates, so does everything else.

The documentation has a little more to say on the matter:

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.

Kevin
  • 74,910
  • 12
  • 133
  • 166
  • 1
    @Tommy If you don't *need* to gracefully shut down your background threads, the approach given here is fine. If you do, then doing what you're doing your example is the right way. – dano Nov 20 '15 at 19:14
  • I guess one minor flaw with my approach is that it does not kill the current processing in the While loop. It does not receive a kill event that "rolls" back this loop as a transaction. I've edited my question with this problem. – Tommy Nov 20 '15 at 19:15