python中有哪些线程调用的方式
这期内容当中小编将会给大家带来有关python中有哪些线程调用的方式,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

创新互联建站主营尼开远网站建设的网络公司,主营网站建设方案,App定制开发,尼开远h5成都微信小程序搭建,尼开远网站营销推广欢迎尼开远等地区企业咨询
1 直接调用
import threading
import time
def sayhi(num): #定义每个线程要运行的函数
print("running on number:%s" %num)
time.sleep(3)
if __name__ == '__main__':
t1 = threading.Thread(target=sayhi,args=(1,)) #生成一个线程实例
t2 = threading.Thread(target=sayhi,args=(2,)) #生成另一个线程实例
t1.start() #启动线程
t2.start() #启动另一个线程
print(t1.getName()) #获取线程名
print(t2.getName())
2 间接调用
import threading
import time
class MyThread(threading.Thread):
def __init__(self,num):
threading.Thread.__init__(self)
self.num = num
def run(self):#定义每个线程要运行的函数
print("running on number:%s" %self.num)
time.sleep(3)
if __name__ == '__main__':
t1 = MyThread(1)
t2 = MyThread(2)
t1.start()
t2.start()上述就是小编为大家分享的python中有哪些线程调用的方式了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注创新互联行业资讯频道。
网页标题:python中有哪些线程调用的方式
网站地址:http://www.jxjierui.cn/article/gppdpj.html


咨询
建站咨询
