python中thread的setDaemon、join的用法

http://blog.csdn.net/changzhi1990/article/details/20624883

python中得thread的一些机制和C/C++不同:在C/C++中,主线程结束后,其子线程会默认被主线程kill掉。而在python中,主线程结束后,会默认等待子线程结束后,主线程才退出。

        python对于thread的管理中有两个函数:join和setDaemon

  • join:如在一个线程B中调用threada.join(),则threada结束后,线程B才会接着threada.join()往后运行。
  • setDaemon:主线程A启动了子线程B,调用b.setDaemaon(True),则主线程结束时,会把子线程B也杀死,与C/C++中得默认效果是一样的。

import threading  
import time  
   
class myThread(threading.Thread):  
   def __init__(self, threadname):  
     threading.Thread.__init__(self, name=threadname)  
     self.st = 2   
    
   def run(self):  
     time.sleep(self.st)  
     print self.getName()  
   def setSt(self, t):   
     self.st = t   
    
 def fun1():  
   t1.start()  
   print "fun1 done"  
    
 def fun2():  
   t2.start()  
   print "fun2 done"  
    
 t1=myThread("t1")  
 t2=myThread("t2")  
 t2.setSt(10);  
 # t2.setDaemon(True)  
 fun1()  
 fun2()  
 print "now u will see me"  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值