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

最新文章

  1. java入门第一季2
  2. 快速熟悉python 下使用mysql(MySQLdb)
  3. android 拨号
  4. Oracle中 根据 file# 和 block# 找到对象
  5. .NET软件汉化小实例
  6. 对libssh的golang 封装 ssh2go
  7. 深入剖析C/C++函数的参数传递机制
  8. BZOJ 1618: [Usaco2008 Nov]Buying Hay 购买干草( dp )
  9. AIX上面Oracle数据库相关启动
  10. 微信小程序(有始有终,全部代码)开发--- 新增【录音】以及UI改进
  11. Java开发API文档资源
  12. Python基础语法 系统学习
  13. 清理buffer/cache/swap的方法梳理
  14. Python档案袋( 进程与协程 )
  15. (三)Installation
  16. IPv6升级改造包括什么?
  17. aop编程之后置通知,环绕通知和异常通知
  18. vue项目中, 字段信息为空时不渲染,是真的不渲染吗
  19. postman断言的几种方式(二)
  20. Luogu1041 NOIP2003传染病控制(搜索)

热门文章

  1. Vue开发实战
  2. 【BZOJ2938】【luoguP2444】病毒
  3. The chance for love doesn't come around every day.
  4. php 即点即改
  5. bzoj 2751
  6. Delphi exe实例之间传递cmd参数
  7. NX二次开发-UFUN替换组件UF_ASSEM_substitute_component
  8. Spring-Security (学习记录二)--修改为自己的登录页面
  9. Python codecs小Tips
  10. AtCoder ABC 128F Frog Jump