乱读天书, 不求甚解
周祎骏的个人云笔记
Toggle navigation
乱读天书, 不求甚解
主页
Linux:系统配置
Linux:用户管理
Linux:优化排错
Linux:进程调度
Linux:文件系统
Linux:网络
Linux:系统服务
Linux:安全
Linux:内核
容器:Docker
容器:containerd
容器编排:Kubernetes
IAC:Terraform
大数据:Hadoop
大数据:Zookeeper
大数据:Hbase
消息队列:rsyslog
消息队列:kafka
数据库:MySQL
数据库:MongoDB
搜索引擎:Elasticsearch
时序数据库:OpenTSDB
网站服务:Nginx
编程:Bash
编程:Perl
编程:Python
编程:C
编程:JAVA
编程:Rust
版本控制:gitlab
知识管理:docusaurus
常用小工具
关于我
标签
python 3.040 多线程
2017-05-23 12:48:44
75
0
0
admin
> 这里介绍thread 和threading #thread 模块是轻量级的多线程模块 ``` #!/usr/bin/python import thread,time array=range(0,20) count=len(array) the_lock=thread.allocate_lock() #生成锁对象 def the_fun(var): print str(var)+"==>"+str(thread.get_ident()) #得到线程id global count while True: if the_lock.acquire(): #获得锁成功 count = count - 1 the_lock.release() #释放锁 break else: time.sleep(1) for i in array: thread.start_new_thread(the_fun,(i,)) #创建线程,thread.start_new_thread(调用的函数,(调用函数的参数组成的数组)) while count > 0: #不这样主线程会退出,其余线程在主线程退出的时候被kill pass ``` *** #threading 是高级多线程模块 ##简单使用: ``` #!/usr/bin/python import threading,time array=range(0,20) count=len(array) the_lock=threading.Lock() #生成锁对象 def the_fun(var): print str(var)+"==>"+str(threading.currentThread().ident) #threading.currentThread() 获取当前线程对象 #.ident 获得当前线程的id global count while True: if the_lock.acquire(False): #获得锁,如果获得失败,返回False #如果参数是True,会一直等下去 count = count - 1 the_lock.release() break else: time.sleep(1) for i in array: the_thread=threading.Thread(target=the_fun,args=(i,),name=str(i)) #创建线程 target=调用的函数,args=调用的参数, name=线程名字 #the_thread.setDaemon(True) #设置为后台线程 #前台线程:在主线程退出时,会等待所有前台线程 #后台线程:在主线程退出时,不会等待后台线程,后台线程会被kill print the_thread.isDaemon() #是否是后台线程 the_thread.start() #开始 print the_thread.isAlive() #是否还在运行 #the_thread.setName("xx") #改名字 print the_thread.getName() #获得名字 while count > 0: print "current_thread_num:"+str(threading.activeCount()) #获得当前线程数量 time.sleep(1) ``` ##关于RLock RLock 可以被同一个线程acquire 多次。 ``` >>> a = threading.RLock() >>> a <_RLock owner=None count=0> >>> a.acquire() True >>> a <_RLock owner='MainThread' count=1> >>> a.acquire() 1 >>> a <_RLock owner='MainThread' count=2> >>> a.release() >>> a <_RLock owner='MainThread' count=1> >>> ``` ##关于信号量Semaphore 控制同时有多少个线程在跑 ``` #!/usr/bin/python import threading,time array=range(0,20) def the_fun(var): semaphore.acquire() #相当于获得锁 print str(var)+"==>"+str(threading.currentThread().ident) time.sleep(1) semaphore.release() #相当于释放锁 semaphore = threading.BoundedSemaphore(5) #同时最多运行5个线程 for i in array: the_thread=threading.Thread(target=the_fun,args=(i,)) the_thread.start() while threading.activeCount() > 1: time.sleep(1) ``` ##定时器Timer 在等待指定时间后触发线程 ``` the_thread=threading.Timer(5,the_fun,args=(i,)) #Timer(时间,调用的函数,调用函数的参数) ``` ##事件event 主线程控制其它线程的方法,方式就是定义一个全局的布尔值。 set将其设置为True,clear将其设置为False wait(timeout) 直到event为true再运行下去 ``` #!/usr/bin/python import threading,time array=range(0,20) def the_fun(var): print str(var)+"==>"+str(threading.currentThread().ident)+"==>start" event.wait() print str(var)+"==>"+str(threading.currentThread().ident)+"==>end" event=threading.Event() for i in array: the_thread=threading.Thread(target=the_fun,args=(i,)) the_thread.start() print event.isSet() #查看是否为True time.sleep(5) print "set event" event.set() while threading.activeCount() > 1: time.sleep(1) ``` ##条件变量condition 通常用于一个线程完成自己的工作以后唤醒另一个线程 ``` #!/usr/bin/python import threading,time con = threading.Condition() number = 0 class add_num(threading.Thread): def __init__(self): super(add_num,self).__init__() def run(self): #重写run 函数 global number con.acquire() #获得锁 while True: print str(number)+"==>+1" number = number + 1 if number == 5: con.notify() #通知其他线程 #con.notify(2) #通知2个线程 #con.notifyAll() #通知所有线程 con.wait() #等待被唤醒 con.release() #释放锁 class reduce_num(threading.Thread): def __init__(self): super(reduce_num,self).__init__() def run(self): global number con.acquire() while True: print str(number)+"==>-1" number = number - 1 if number == 0: con.notify() con.wait() con.release() a = add_num() r = reduce_num() a.start() r.start() ```
上一篇:
python 3.031 commands 模块
下一篇:
python 3.041 多进程
文档导航