标签:daemon ogr demo 结束 initial status not author 注意
# 属性进行封装 以方便对数据传入值合法性进行判断 @daemon.setter # 将self._daemonic def daemon(self, daemonic): if not self._initialized: raise RuntimeError("Thread.__init__() not called") if self._started.is_set(): raise RuntimeError("cannot set daemon status of active thread") self._daemonic = daemonic # 判断此线程是否是守护线程 def isDaemon(self): return self.daemon #通过方法修改daemonic属性值 不建议使用 def setDaemon(self, daemonic): self.daemon = daemonic
1 # encoding:utf-8 2 # Author:"richie" 3 # Date:8/29/2017 4 # python线程:线程的调度-守护线程 5 6 from threading import Thread,Timer 7 import time 8 9 class MyCommon(Thread): 10 11 def run(self): 12 for i in range(1,5): 13 t = Timer(7, self.print_msg, args=(i,)) 14 t.start() 15 16 def print_msg(self,i): 17 print("线程1第" + str(i) + "次执行!") 18 19 class MyDaemon(Thread): 20 """ 21 守护线程不可以在重新new thread 22 """ 23 def run(self): 24 for i in range(9999999): 25 print("后台线程第" + str(i) + "次执行!") 26 time.sleep(2) 27 28 if __name__ == ‘__main__‘: 29 t1 = MyCommon() 30 t2 = MyDaemon() 31 t2.deamon = True # 设置为守护线程 32 t1.start() 33 t2.start()
D:\Python\Python3.6\python.exe D:/Python/PythonProgram/day35/案例分析.py
后台线程第0次执行!
后台线程第1次执行!
后台线程第2次执行!
后台线程第3次执行!
线程1第1次执行!
线程1第2次执行!
线程1第3次执行!
线程1第4次执行!
Process finished with exit code 0
标签:daemon ogr demo 结束 initial status not author 注意
原文地址:http://www.cnblogs.com/richiewlq/p/7447604.html