python多线程模块threading学习笔记(2)
2021/4/8 14:26:02
本文主要是介绍python多线程模块threading学习笔记(2),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
参考链接: 【莫烦Python】Threading 学会多线程 Python
参考链接: 莫烦 多线程
参考链接: threading — 基于线程的并行
testThreading3_1.py
# 教学视频第3集 import threading # 导入必要的包 import time def thread_job(): print('T1 start\n') for i in range(10): time.sleep(0.1) print('T1 finished\n') def main(): added_thread = threading.Thread( target=thread_job, name = 'CXQ20200910SCM' ) added_thread.start() print('All Done!!!\n') if __name__ == '__main__': main() r''' 输出结果,情形1: All Done!!! T1 start T1 finished 输出结果,情形2: T1 start All Done!!! T1 finished '''
testThreading3_2.py
# 教学视频第3集 import threading # 导入必要的包 import time def thread_job(): print('T1 start\n') for i in range(10): time.sleep(0.1) print('T1 finished\n') def main(): added_thread = threading.Thread( target=thread_job, name = 'CXQ20200910SCM' ) added_thread.start() added_thread.join() # 等待,直到线程终结。 print('All Done!!!\n') if __name__ == '__main__': main() r''' 输出结果: T1 start T1 finished All Done!!! '''
testThreading3_3.py
# 教学视频第3集 import threading # 导入必要的包 import time def thread_job(): print('T1 start\n') for i in range(10): time.sleep(0.1) print('T1 finished\n') def job_T2(): print('T2 start!!!\n') print("T2 finished!!!\n") def main(): added_thread = threading.Thread( target=thread_job, name = 'CXQ20200910SCM' ) T2 = threading.Thread( target=job_T2, name='T2_20200910' ) added_thread.start() T2.start() added_thread.join() # 等待,直到线程终结。 T2.join() print('All Done!!!\n') if __name__ == '__main__': main() r''' 输出结果: T1 start T2 start!!! T2 finished!!! T1 finished All Done!!! '''
testThreading3_4.py
# 教学视频第3集 import threading # 导入必要的包 import time def thread_job(): print('T1 start\n') for i in range(10): time.sleep(0.1) print('T1 finished\n') def job_T2(): print('T2 start!!!\n') print("T2 finished!!!\n") def main(): added_thread = threading.Thread( target=thread_job, name = 'CXQ20200910SCM' ) T2 = threading.Thread( target=job_T2, name='T2_20200910' ) added_thread.start() T2.start() added_thread.join() # 等待,直到线程终结。 print('All Done!!!\n') if __name__ == '__main__': main() r''' 输出结果: T1 start T2 start!!! T2 finished!!! T1 finished All Done!!! '''
testThreading3_5.py
# 教学视频第3集 import threading # 导入必要的包 import time def thread_job(): print('T1 start\n') for i in range(10): time.sleep(0.1) print('T1 finished\n') def job_T2(): print('T2 start!!!\n') print("T2 finished!!!\n") def main(): added_thread = threading.Thread( target=thread_job, name = 'CXQ20200910SCM' ) T2 = threading.Thread( target=job_T2, name='T2_20200910' ) added_thread.start() T2.start() # added_thread.join() # 等待,直到线程终结。 T2.join() print('All Done!!!\n') if __name__ == '__main__': main() r''' 输出结果: T1 start T2 start!!! T2 finished!!! All Done!!! T1 finished '''
testThreading3_6.py
# 教学视频第3集 import threading # 导入必要的包 import time def thread_job(): print('T1 start\n') for i in range(10): time.sleep(0.1) print('T1 finished\n') def job_T2(): print('T2 start!!!\n') print("T2 finished!!!\n") def main(): added_thread = threading.Thread( target=thread_job, name = 'CXQ20200910SCM' ) T2 = threading.Thread( target=job_T2, name='T2_20200910' ) added_thread.start() T2.start() # added_thread.join() # 等待,直到线程终结。 # T2.join() print('All Done!!!\n') if __name__ == '__main__': main() r''' 输出结果: T1 start T2 start!!! All Done!!! T2 finished!!! T1 finished '''
这篇关于python多线程模块threading学习笔记(2)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-21Python编程基础教程
- 2024-11-20Python编程基础与实践
- 2024-11-20Python编程基础与高级应用
- 2024-11-19Python 基础编程教程
- 2024-11-19Python基础入门教程
- 2024-11-17在FastAPI项目中添加一个生产级别的数据库——本地环境搭建指南
- 2024-11-16`PyMuPDF4LLM`:提取PDF数据的神器
- 2024-11-16四种数据科学Web界面框架快速对比:Rio、Reflex、Streamlit和Plotly Dash
- 2024-11-14获取参数学习:Python编程入门教程
- 2024-11-14Python编程基础入门