Reprinted from: http://blog.sina.com.cn/s/blog_9f488855010198vn.html
Correct or not verified
Some of the mechanisms of thread in Python are different from C + +: in C + +, when the main thread ends, its child threads are killed by default by the main thread. In Python, after the main thread ends, it waits for the child thread to end by default, and the mainline friend exits.
Python has two functions for thread management: Join and Setdaemon
- join: if Threada.join () is called in a thread B, thread B will then run back Threada.join () after Threada ends.
- Setdaemon: The main thread A starts child thread B and calls B.setdaemaon (True), the thread B is also killed when the main thread ends, the same as the default effect in C + +.
Here is an example:
1 #! /usr/bin/env Python 2 3 import Threading 4 Import time 5 6 class MyThread (threading. Thread): 7 def __init__ (self, threadname): 8 Threading. Thread.__init__ (self, name=threadname) 9 self.st = 2 def run (self): time.sleep (self.st) 13 print Self.getname () def setst (self, T): self.st = t, def fun1 (): T1.start (19) print "Fun1 done"- def fun2 (): t2.start () print "fun2 done" t1=mythread ("T1") T2=mythread ("T2") t2.setst (+); T2.setdaemon (True) fun1 () fun2 () print "Now u'll See Me"
Thread in Python