python多线程模块threading学习总结
来源:互联网 发布:淘宝助理价格设置 编辑:程序博客网 时间:2024/05/21 17:01
python除了thread模块,提供了更高级的threading模块来实现多线程
另外python在多核cpu中为提高并发,提供了multiprocessing多进程
threading模块提供的方法:
1.threading.active_count()
1.threading.activeCount() 获取正在运行的线程数量
2.threading.Condition() 相当于高级锁对象,可以使一个或多个线程等待直到被其他线程调度或通知, <<详情参考>>
3.threading.current_thread()
3.threading.currentThread() 返回当前线程对象
4.threading.enumerate() 返回当前存活即active的线程对象
5.threading.Event() 返回一个事件对象,可用set()将事件消息设置为true,也可用clear(),wait()设置等待,直到设置为true
<<详情参考>>
6.threading.local 是一个class类对象,拥有线程独有的data信息,不同的线程之间values值也不同
更详细的信息可用参考_threading_local模块
7.threading.Lock() 线程的锁对象,是一个基本的锁对象,一次只能一个锁定,其余锁请求,需等待锁释放后才能获取,
<<详情参考>>
7.threading.RLock() 多重锁,在同一线程中可用被多次acquire。如果使用RLock,那么acquire和release必须成对出现,
调用了n次acquire锁请求,则必须调用n次的release才能在线程中释放锁对象, <<详情参考>>
8.threading.Thread 一个线程控制器的对象
9.class threading.Timer 在间隔制定时间后执行线程, <<详情参考>>
10.threading.settrace(func) 在线程调用run()之前为所有的线程开启trace,实际调用的sys.settrace()
11.threading.setprofile(func) 在线程run()之前为线程设置profile属性值
12.threading.stack_size([size]) 堆栈size大小
13.threading.Semaphore([value]) 信号量,<<详情参考>>
13.threading.BoundedSemaphore([value]) 有界信号量,能够保证资源不超过vlaue值,默认1,<<详情参考>>
threading.Thread线程对象
定义:
class threading.Thread(group=None, target=None, name=None, args=(), kwargs={})
target调用对象,name线程名称,args=()参数
提供如下方法:
1.start()
2.run() 、 __init__() 可重写
3.join([timeout]) ,挂起进程,直到结束,也可设置超时时间,但超时后继续执行后续步骤,子进程不会退出,后面的示例会说明
4.getName()
5.setName() 也可以在线程对象创建时传入name参数
6.is_alive() / isAlive()
7.isDaemon()
8.setDaemon() 设置守护线程,<<详情参考>>
线程的一般使用:
start()
join()
示例:
基本的单个线程threading.Thread
#coding=utf8import threadingimport timedef showperson(name): print 'show person name :%s'%nameprint '%s thread start!'%(time.ctime())t =threading.Thread(target=showperson,args=("tom",))t.start()print '%s thread end!'%(time.ctime())
输出:
Sun May 28 16:37:01 2017 thread start!show person name :tomSun May 28 16:37:01 2017 thread end!
改写为多个线程threading.Thread
#coding=utf8import threadingimport timedef showperson(name): print 'show person name :%s'%nameprint '%s thread start!'%(time.ctime())for i in range(3): t =threading.Thread(target=showperson,args=("person-%d"%i,)) t.start()print '%s thread end!'%(time.ctime())
输出:
Sun May 28 16:38:10 2017 thread start!show person name :person-0show person name :person-1show person name :person-2Sun May 28 16:38:10 2017 thread end!
当然也可以根据自己的需要,对threading.Thread进行继承,定制,重写__init__() , run()
#coding=utf8import threadingimport timedef showperson(name): print 'show person name :%s'%nameprint '%s thread start!'%(time.ctime())class ReThreading(threading.Thread): def __init__(self,str): super(ReThreading,self).__init__() self.detial=str def run(self): print "threading detial -- %s"%self.detialfor i in range(3): t =ReThreading("rewrite threading %d"%i) t.start()print '%s thread end!'%(time.ctime())
输出:
Sun May 28 16:51:45 2017 thread start!threading detial -- rewrite threading 0threading detial -- rewrite threading 1threading detial -- rewrite threading 2 Sun May 28 16:51:45 2017 thread end!
getName()获取线程名称:
获取当前线程名称threading.currentThread().getName()
#coding=utf8import threadingimport timedef showperson(name): print 'show person name :%s'%nameprint '%s thread start!'%(time.ctime())for i in range(3): t =threading.Thread(target=showperson,args=("person-%d"%i,)) print "threading name : %s"%(t.getName()) t.start()print '%s thread end!'%(time.ctime())
输出:
Sun May 28 17:04:16 2017 thread start!threading name : Thread-1show person name :person-0threading name : Thread-2show person name :person-1threading name : Thread-3show person name :person-2Sun May 28 17:04:16 2017 thread end!
threading内部会为线程设置默认的名称,Thread-%d 从1开始
设置线程名称,
1.将t线程初始化修改为:
t =threading.Thread(target=showperson,name=”Th-%d”%i,args=(“person-%d”%i,))
2使用setName方法
t.setName(“Th-%d”%i)
判断线程是否存活
1.print t.is_alive()
2.print t.isAlive()
判断是否是守护线程
print t.isDaemon()
setDaemon设置守护线程,后面在死循环结束方法进行说明
如下:
#coding=utf8import threadingimport timedef showperson(name): print 'show person name :%s'%nameprint '%s thread start!'%(time.ctime())for i in range(3): t =threading.Thread(target=showperson,args=("person-%d"%i,)) t.setName("Th-%d"%i) print "threading name : %s"%(t.getName()) t.start() print t.is_alive() print t.isAlive() print t.isDaemon()print '%s thread end!'%(time.ctime())
输出:
Sun May 28 17:32:39 2017 thread start!threading name : Th-0show person name :person-0 TrueFalseFalsethreading name : Th-1show person name :person-1 TrueFalseFalsethreading name : Th-2show person name :person-2FalseFalseFalseSun May 28 17:32:39 2017 thread end!
多执行几次,可以看到偶尔线程状态ture或false,这是线程执行完毕后,回收注销线程有时间间隔
可以添加time.sleep(0.01),但是对执行速率有一定的影响
join将线程挂起
对子线程进行管理
如下主进程与子线程自己的协调问题:
#coding=utf8import threadingimport timedef showperson(name): time.sleep(1) print 'show person name :%s'%nameprint '%s thread start!'%(time.ctime())for i in range(3): t =threading.Thread(target=showperson,args=("person-%d"%i,)) t.setName("Th-%d"%i) print "threading name : %s"%(t.getName()) t.start()print '%s thread end!'%(time.ctime())
输出:
Sun May 28 17:57:58 2017 thread start!threading name : Th-0threading name : Th-1threading name : Th-2Sun May 28 17:57:58 2017 thread end!show person name :person-0show person name :person-2show person name :person-1
可以查看到结果,程序已经打印处理输出,而后面的子进程之后才输出结果,这不是想要的结果
如果想让子进程执行完毕后,再打印出程序结束,则需要使用join()将进程挂起:
修改如下:
for i in range(3): t =threading.Thread(target=showperson,args=("person-%d"%i,)) t.setName("Th-%d"%i) print "threading name : %s"%(t.getName()) t.start() t.join()
这里就会有一个新的问题,这样就相当于单线程了,无法达到并发的效果
结果:
Sun May 28 18:02:51 2017 thread start!threading name : Th-0show person name :person-0threading name : Th-1show person name :person-1threading name : Th-2show person name :person-2Sun May 28 18:02:54 2017 thread end!
继续改进,将所有进程start之后再join()挂起进程
如下:
#coding=utf8import threadingimport timedef showperson(name): time.sleep(1) print 'show person name :%s'%nameprint '%s thread start!'%(time.ctime())list=[]for i in range(3): t =threading.Thread(target=showperson,args=("person-%d"%i,)) t.setName("Th-%d"%i) print "threading name : %s"%(t.getName()) list.append(t) t.start()for threadinglist in list: threadinglist.join()print '%s thread end!'%(time.ctime())
输出:
Sun May 28 18:05:42 2017 thread start!threading name : Th-0threading name : Th-1threading name : Th-2show person name :person-0show person name :person-1show person name :person-2Sun May 28 18:05:43 2017 thread end!
注:join([timeout]),不会退出子线程
如下示例:
#coding=utf8import threadingimport timedef showperson(name): time.sleep(2) print 'show person name :%s'%nameprint '%s thread start!'%(time.ctime())list=[]for i in range(3): t =threading.Thread(target=showperson,args=("person-%d"%i,)) t.setName("Th-%d"%i) print "threading name : %s"%(t.getName()) list.append(t) t.start()class MyException(Exception): def __init__(self,message): Exception.__init__(self) self.message=messagefor threadinglist in list: threadinglist.join(0.1) #设置0.1秒超时时间print '%s thread end!'%(time.ctime())for threadinglist in list: try: if threadinglist.isAlive(): raise MyException("%s timeout"%threadinglist) except MyException,e: print e.message
结果:
Sun May 28 19:32:41 2017 thread start!threading name : Th-0threading name : Th-1threading name : Th-2Sun May 28 19:32:41 2017 thread end!<Thread(Th-0, started 704)> timeout<Thread(Th-1, started 5700)> timeout<Thread(Th-2, started 8068)> timeoutshow person name :person-0show person name :person-1show person name :person-2
当print end打印后,即说明主程序退出,在末尾添加if判断子线程是否active,
若线程还存活,则抛出一个自定义的timeout异常
- python多线程模块threading学习总结
- python多线程模块threading学习
- Python 多线程 Threading模块
- python threading多线程模块
- python多线程-threading模块
- python多线程模块threading
- Python多线程(threading)学习总结
- python--threading多线程总结
- python--threading多线程总结
- 4.python学习笔记:python多线程下的threading模块
- python多线程模块threading简例
- [Python]多线程--threading模块实现
- Python 多线程(threading模块)
- Python 多线程(threading模块)
- Python的多线程threading模块
- python多线程编程----threading模块
- python模块threading实现多线程
- Python 多线程编程threading模块
- 自定义airline
- 什么是默认构造函数?
- 无主之地2 SaveEdit 使用说明
- applicationContext配置---MyBatis
- AVL树
- python多线程模块threading学习总结
- git入门基础教程以及idea中实战
- 接口中的嵌套实现代码
- 类的静态数据成员在 类内只能声明,定义和初始化必须在类外
- 记录
- SpringMVC学习笔记(七):Ajax用法一(传非对象的数据)
- 如愿以偿获得Google(北京)技术岗工作
- 6.5 About internet telephony
- JAVA 变量认识