简体   繁体   中英

Are daemon threads killed when main thread calls sys.exit()?

According to the documentation: https://docs.python.org/3/library/threading.html

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 or the daemon constructor argument.

Sample code:

thread = threading.Thread(target=MultiHandler().network, args=(conn, data), daemon=True)
thread.start()

Refering to many other StackOverflow answers, it is not clear to me if daemon threads are forced to close when the main thread calls sys.exit()

Referring to the comment posted by zwer,

When a program exits, all of its children threads are killed with it. Threads that are not daemonic will prevent the program from exiting, hence preventing their own destruction. - zwer

In short, yes daemon threads will not stop the program from exiting thus they will be killed on exit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM