Daemon 守护线程(27-11)
2024-09-06 10:21:23
t2.setDaemon(True)不再等待里面的sleep(5)。
当设成setDaemon(True)这个线程就不等了。
例子一:
import threading
from time import ctime, sleep def music(func):
for i in range(2):
print(func, ctime()) # 1 执行 # 5 执行
sleep(1)
print("end music", ctime()) # 4 执行 # 6 执行 def move(func):
for i in range(2):
print(func, ctime()) # 2 执行
sleep(5)
print("end move", ctime())
threads = []
t1 = threading.Thread(target=music,args=("小苹果",))
threads.append(t1)
t2 = threading.Thread(target=move,args=("华尔街之狼",))
threads.append(t2)
if __name__ == "__main__":
t2.setDaemon(True)
for t in threads:
t.start()
print("程序执行结束", ctime()) # 3 执行
程序运行结果:
小苹果 Fri Sep 7 20:04:24 2018
华尔街之狼 Fri Sep 7 20:04:24 2018
程序执行结束 Fri Sep 7 20:04:24 2018
end music Fri Sep 7 20:04:25 2018
小苹果 Fri Sep 7 20:04:25 2018
end music Fri Sep 7 20:04:26 2018
------------------------------------------------------------------------------------------------------
例子二:
t.setDaemon(True)谁也不等待
import threading
from time import ctime, sleep def music(func):
for i in range(2):
print(func, ctime()) # 1 执行
sleep(1)
print("end music", ctime()) def move(func):
for i in range(2):
print(func, ctime()) # 2 执行
sleep(5)
print("end move", ctime())
threads = [] t1 = threading.Thread(target=music,args=("小苹果",)) threads.append(t1) t2 = threading.Thread(target=move,args=("华尔街之狼",)) threads.append(t2) if __name__ == "__main__":
for t in threads:
t.setDaemon(True)
t.start()
print("程序执行结束", ctime()) # 3 执行
程序运行结果:
小苹果 Fri Sep 7 20:18:31 2018
华尔街之狼 Fri Sep 7 20:18:31 2018
程序执行结束 Fri Sep 7 20:18:31 2018
最新文章
- java入门第一季2
- 快速熟悉python 下使用mysql(MySQLdb)
- android 拨号
- Oracle中 根据 file# 和 block# 找到对象
- .NET软件汉化小实例
- 对libssh的golang 封装 ssh2go
- 深入剖析C/C++函数的参数传递机制
- BZOJ 1618: [Usaco2008 Nov]Buying Hay 购买干草( dp )
- AIX上面Oracle数据库相关启动
- 微信小程序(有始有终,全部代码)开发--- 新增【录音】以及UI改进
- Java开发API文档资源
- Python基础语法 系统学习
- 清理buffer/cache/swap的方法梳理
- Python档案袋( 进程与协程 )
- (三)Installation
- IPv6升级改造包括什么?
- aop编程之后置通知,环绕通知和异常通知
- vue项目中, 字段信息为空时不渲染,是真的不渲染吗
- postman断言的几种方式(二)
- Luogu1041 NOIP2003传染病控制(搜索)
热门文章
- Vue开发实战
- 【BZOJ2938】【luoguP2444】病毒
- The chance for love doesn't come around every day.
- php 即点即改
- bzoj 2751
- Delphi exe实例之间传递cmd参数
- NX二次开发-UFUN替换组件UF_ASSEM_substitute_component
- Spring-Security (学习记录二)--修改为自己的登录页面
- Python codecs小Tips
- AtCoder ABC 128F Frog Jump