导语:
本文主要介绍了关于python多线程中的threading使用技巧的相关知识,希望可以帮到处于编程学习途中的小伙伴
在任何区域设置中,总有一个掌控全局的管理者。这也是我们在公司需要一个leader来统筹布局的道理。
在python多线程中,还有一个方法有着如此重要的作用——threading,相信你已经看过很多次了,你知道这个方法有哪些实用的功能吗?为什么大家都选择它?如果还是理解认知不清晰,可以继续阅读正文。
threading模块
的主要应用:
多线程启动
# 多线程启动
import os
import time
from threading import Thread
def func():
time.sleep(1)
print('hello 线程', os.getpid())
t = Thread(target=func)
t.start()
print(os.getpid())
# 结果
# 6360
# hello 线程 6360
同步开启多线程
# 同步开启多线程
import os
import time
from threading import Thread
def func():
time.sleep(1)
print('hello 线程', os.getpid())
thread_l = []
for i in range(10):
t = Thread(target=func)
t.start()
thread_l.append(t)
for j in thread_l:
j.join()
print(os.getpid())
如果需要开启多线程运行,可以直接调用该模块。上面已经为你提供了演示代码。如果还需要代码解释,可以直接套用上面的内容。
本文为原创文章,版权归知行编程网所有,欢迎分享本文,转载请保留出处!
你可能也喜欢
- ♥ python帮助文档的使用方法11/27
- ♥ python 中的 Plotly Express 是什么?12/07
- ♥ python中的%s是什么08/14
- ♥ python类和函数有什么区别09/06
- ♥ python数值类型的使用01/11
- ♥ 如何在python中编写矩阵转置11/15
内容反馈