0

How to force main thread to close if a sub thread is alive in python?

sys.exit() and exit() appears to wait for sub thread to complete?

K DawG
  • 13,287
  • 9
  • 35
  • 66
user3388884
  • 4,748
  • 9
  • 25
  • 34
  • Might be because the sub-threads are set as non-daemon threads. Not sure if this would block the execution from stopping however. Might be worth looking into. If you're using the Threading module there's an option in initialization to determine its daemon status. By default all Threading processes inherit their parent thread status. Since the parent is main they will be set to non-daemon. To change this you would set it to daemon=False during initialization. – Bob Jun 06 '14 at 20:18
  • To quote one of the answers: "Without daemon threads, you'd have to keep track of them, and tell them to exit, before your program can completely quit." – Rob Watts Jun 06 '14 at 20:21
  • I see... well the idea was to kill the main thread in order to kill the sub threads... will this be anyway possible in python? – user3388884 Jun 06 '14 at 20:29
  • @user3388884 If you want the sub threads to die when the main thread dies, make your sub threads daemon threads: `subthread.daemon = True`. You have to set them as daemon before you call `start()` on them. – Rob Watts Jun 06 '14 at 20:42
  • You should look at this other post on SO [Is there a way to kill a Thread in Python](http://stackoverflow.com/questions/323972/is-there-any-way-to-kill-a-thread-in-python). You will find all the elements for your question. – Serge Ballesta Jun 06 '14 at 20:44
  • thanks for all the suggestions... I might find my solution under that link... @RobWatts to my understanding, when daemon threads will not die if main thread dies right? – user3388884 Jun 06 '14 at 20:58

2 Answers2

2

Turn your sub threads into daemon threads. For example:

class SubThread(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        self.daemon = True # Makes the thread a daemon thread
        ...

Or

subThread = threading.Thread(...)
subThread.daemon = True

When your program ends, all daemon threads will immediately die. If the sub threads are not daemon threads, then they must be stopped before your program will end. As Serge mentioned in a comment, "Is there any way to kill a Thread in Python?" talks about ways to stop threads.

Community
  • 1
  • 1
Rob Watts
  • 6,866
  • 3
  • 39
  • 58
1

You may want to look into setting it as a daemon thread. See information about daemon threads at Daemon Threads Explanation. Then it shouldn't wait for that thread to complete.

Community
  • 1
  • 1
DivineWolfwood
  • 1,752
  • 12
  • 20
  • thanks so much everyone... I completely misunderstood characteristics of daemon threads. – user3388884 Jun 06 '14 at 22:39
  • But what function do I use to actually exit the main program? I'm using sys.exit(), but it doesn't seem to exit the main thread even when sub thread are set to Daemon... – user3388884 Jun 07 '14 at 00:33