0
votes

Is it good practice to exit a thread manually? The below code has commented #sys.exit() because I assume the thread ends there. But is it good practice to end it anyway? If not, is there any overhead created if that thread is called hundres of times without manually exiting with sys.exit()?

import time
import threading
import sys

def non_daemon():
    print('Test non-daemon')
    time.sleep(5)
    #sys.exit() # is this necessary/encouraged?

t = threading.Thread(name='non-daemon', target=non_daemon)
t.start()

print("done")
1

1 Answers

4
votes

sys.exit() has nothing to do with threads. As the docs explain, it only has an effect when called from the main thread. In all other cases, all it does is raise an exception. There's absolutely no reason to call it in a thread.