帝王谷资源网 Design By www.wdxyy.com
我就废话不多说了,还是直接看代码吧!
from time import ctime import threading import time def a(): #for i in range(5): print('Program a is running... at ', ctime(),u'.线程名为:',threading.current_thread().name ) time.sleep(0.2) def b(x): #for i in range(5): print('Program b('+x+') is running... at ', ctime(),u'.线程名为:',threading.current_thread().name ) time.sleep(0.1) if __name__ == '__main__': print('Mainthread %s is running...' % threading.current_thread().name) thread_list = [] for i in range(400):#同时运行多个 t1= threading.Thread(target=a) thread_list.append(t1) t2 = threading.Thread(target=b, args=('Python',)) thread_list.append(t2) t3 = threading.Thread(target=b, args=('Java',)) thread_list.append(t3) for t in thread_list: t.setDaemon(True) # 设置为守护线程,不会因主线程结束而中断 t.start() for t in thread_list: t.join() # 子线程全部加入,主线程等所有子线程运行完毕 print('Mainthread %s ended.' % threading.current_thread().name)
补充知识:Python主线程结束为什么守护线程还在运行?
在实际的交互模式中,主线程只有在Python退出时才终止,所以action函数输出结果还是被打印出来了。”
按照我的理解应该是说,在shell里主线程在输出结果之后并没有真的结束,所以action还会打印结果。
建议把程序编译出来,放到另外的环境中测试,估计就会是你要的结果了。
以上这篇Python多线程:主线程等待所有子线程结束代码就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
帝王谷资源网 Design By www.wdxyy.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
帝王谷资源网 Design By www.wdxyy.com
暂无评论...
更新日志
2024年10月25日
2024年10月25日
- 群星2013-青春缤纷辑压箱宝大公开3CD2[新加坡限量版][WAV整轨]
- 林育群.2013-BalladShow(日本版)【环球】【WAV+CUE】
- 陈加洛.1992-痛到感觉不到【宝丽金】【WAV+CUE】
- 群星.2023-宿命之敌电视剧原声带【韶愔音乐】【FLAC分轨】
- 東京事変-大発見[FLAC+CUE]
- 椎名林檎-三文ゴシップ[FLAC+CUE]
- 2024年08月04日
- 裘德《裘德「最后的水族馆」演唱会LIVE》[320K/MP3][228.89MB]
- 裘德《裘德「最后的水族馆」演唱会LIVE》[24bit 48kHz][FLAC/分轨][2.08G]
- 基因三重奏《如果你什么都不说 音乐会现场录音》[320K/MP3][145.37MB]
- 孟庭苇.1996-月亮说话(2020环球24KGOLD限量版)【上华】【WAV+CUE】
- 群星.1997-新艺宝优质音响系列·国语精选监听版【新艺宝】【WAV+CUE】
- 阿桑.2005-寂寞在唱歌(星外星引进版)【华研国际】【WAV+CUE】
- 基因三重奏《如果你什么都不说 音乐会现场录音》[FLAC/分轨][287.43MB]
- 蔡题谦《我爱你,却依然要看你走》[320K/MP3][88.65MB]