multithreading - Python daemon thread does not exit when parent thread exits -
I have some python code which creates a monster thread parent's thread ends almost immediately, but Damon Thread The print keeps sleeping.
Import threading import time_date (): _ (1, 600) for category: time.sleep (1) print ("sleep") def main (): thread = threading . Thread (target = int_sleep) thread.daemon = True thread.start () time.sleep (2) print ("main thread end ...") thread = threading. Thread (target = main) thread Start ()
sys.version:
'3.3.3 (v3.3.3: c3896275c0f6, November 18, 2013, 21:19:30) [ MSc v.1600 64bit (AMD64)] '
Prints:
The main thread of gold end .. < / Div>
= "text"> If you want to thread.daemon = True
, the program will only close when the daemon is left only. The commands sent to Stdout are lost.
main.py
import At the time of importing, def int_sleep (): Add it to a file named (Category: 1, 600): time.sleep ( 1) Print ("sleep") def main (): thread = threading. Thread (target = int_sleep) thread.daemon = True thread.start () time.sleep (2) Print ("main thread end ...") Thread = Threading. Thread (target = main) thread.daemon = True thread.start ()
Run it like this:
el @ apollo: ~ / Code / Python / run01 $ python --version Python 2.7.6 L @ Apollo: ~ $ Python main.py l Apollo: ~ $
It does not print anything because the thread is started. You have set it to a daemon and it has started. Then the program ended.
Additional Notes: If you paste this code into a dragon interpreter, then all print statements will appear on the terminal because the daemon never loses its connection with stdout.
Read more:
Comments
Post a Comment