python线程
实例1
import threading #线程 import time def Hi(num): print("线程总用时:%d sec --> start "%num) time.sleep(num) #等待时间 print("线程结束%d" % num) if __name__ == '__main__': t1=threading.Thread(target=Hi,name='thread1',args=(5,))#创建了一个线程对象t1 t2 = threading.Thread(target=Hi,name='thread2',args=(10,)) # 创建了一个线程对象t1 t2.setDaemon(True) #把t2设置成守护线程,主进程退出时,强制退出此线程,慎用,特别是文档操作或持续交互性的 print('t1线程名称:',t1.getName()) print('t2线程名称:',t2.getName()) t1.start() t2.start() #join(timeout=None) 让当前调用者线程(一般是主线程)等待,直到此线程结束,才会继续调用者线程 t1.join(timeout=6) print("ending..........") t1线程名称: thread1 t2线程名称: thread2 线程总用时:5 sec --> start 线程总用时:10 sec --> start 线程结束5 ending..........
实例2
import threading from time import ctime,sleep import time def ListenMusic(name): '''听音乐线程''' print ("音乐名称:%s 开始听音乐时间:%s" %(name,ctime())) sleep(3) print("结束听音乐时间:%s"%ctime()) def RecordBlog(title): '''写博客线程''' print ("博客名称:%s 开始写博客时间:%s" %(title,ctime())) sleep(5) print('结束写博客时间: %s'%ctime()) threads = [] #创建线程列表 t1 = threading.Thread(target=ListenMusic,name='thread1',args=('忘情水',)) #创建线程实例并传参,注意最后要多一个逗号 t2 = threading.Thread(target=RecordBlog,name='thread2',args=('python线程使用方法',)) threads.append(t1) #添加线程列表1 threads.append(t2) #添加线程列表2 if __name__ == '__main__': t2.setDaemon(True) #t2总用时5秒>t1用时3,可以证明主线程退出时强制退出守护线程 for t in threads: t.start() #启动线程,这里的开启只是告诉cpu,已经准备好了,有时间就来执行我吧 time.sleep(1) print('线程名称:',t.getName()) print("线程数量:",threading.active_count()) #如果没有下面代码,此程序基本跟实例1一样 #下面代码主要体现 active_count() 查看当前在线线程数 n=0 while n<6: time.sleep(1) print('当前线程数:%s 当前时间:%s'%(threading.active_count(),ctime())) if threading.active_count() == 1: print("<---所有进程结束 %s" % ctime()) break n+=1 音乐名称:忘情水 开始听音乐时间:Tue Nov 30 23:36:39 2021 线程名称: thread1 线程数量: 2 博客名称:python线程使用方法 开始写博客时间:Tue Nov 30 23:36:40 2021 线程名称: thread2 线程数量: 3 结束听音乐时间:Tue Nov 30 23:36:42 2021 当前线程数:2 当前时间:Tue Nov 30 23:36:42 2021 当前线程数:2 当前时间:Tue Nov 30 23:36:43 2021 当前线程数:2 当前时间:Tue Nov 30 23:36:44 2021 结束写博客时间: Tue Nov 30 23:36:45 2021 当前线程数:1 当前时间:Tue Nov 30 23:36:45 2021 <---所有进程结束 Tue Nov 30 23:36:45 2021
实例3, 继承线程类,一般不建议使用,但也要能看懂别人的用法
import threading from time import ctime,sleep import time def ListenMusic(name): print ("Begin listening to %s. %s" %(name,ctime())) sleep(3) print("end listening %s"%ctime()) def RecordBlog(title): print ("Begin recording the %s! %s" %(title,ctime())) sleep(5) print('end recording %s'%ctime()) 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() print("ending......") running on number:1 running on number:2 ending......
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库