Python semaphore evevt生产者消费者模型原理解析
作者:flag_HW 时间:2021-11-14 12:52:39
线程锁相当于同时只能有一个线程申请锁,有的场景无数据修改互斥要求可以同时让多个线程同时运行,且需要限制并发线程数量时可以使用信号量
import threading, time, queue
def test(name):
semaphore.acquire() #获取信号量锁
print('my name is %s' %name)
time.sleep(1)
semaphore.release() #释放信号量锁
semaphore = threading.BoundedSemaphore(5) #创建一个信号量同时可以运行3个线程
for i in range(20):
t = threading.Thread(target=test, args=(i,))
t.start()
while threading.active_count() == 1:
print("all run done")
两个或者多个线程需要交互时,且一个进程需要根据另一线程状态执行对应操作时,可以通过event来设置线程状态达到期望的效果,下面是一个红绿灯的例子
event = threading.Event() #实例化一个event
def light():
while True:
print("红灯亮了,请停车")
time.sleep(20) #开始是红灯20s
event.set() #红灯时间到了,设置标志位
print("绿灯亮了,请通行")
time.sleep(30) #持续30s红灯
event.clear() #清空标志位
def car(num):
while True:
if event.is_set():#检测event被设置则执行
print("car %s run"%num)
time.sleep(5)
else:
print("this is red light waiting")
event.wait() #此处会卡主,直到状态被设置才会向下执行
Light = threading.Thread(target=light,)
Light.start()
for i in range(10):
Car = threading.Thread(target=car, args=(i,))
Car.start()
当多个线程需要交互数据可以使用queue来进行数据传递,下面是经典的生产者消费者多线程模型示例,其中包含线程queue的基本使用方法
my_queue = queue.Queue() #实例化一个队列
queue1 = queue.LifoQueue() #后进 先出队列
queue2 = queue.PriorityQueue() #带优先级的队列
def pro():
for i in range(100):
my_queue.put(i) #队列里面放数据
def con():
while my_queue.qsize() > 0: #当队列有数据时候从队列取数据
print("i an a consumer,get num %s"%my_queue.get(timeout=3))
time.sleep(2)
else:
print("my queue is empty")
Pro = threading.Thread(target=pro)
Pro.start()
for j in range(10):
Con = threading.Thread(target=con)
Con.start()
来源:https://www.cnblogs.com/flags-blog/p/12514580.html
标签:Python,semaphore,evevt,生产,消费,模型
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
在Python中等距取出一个数组其中n个数的实现方式
2023-10-18 10:03:10
![](https://img.aspxhome.com/file/2023/4/86164_0s.jpg)
用Python自动下载网站所有文件
2021-02-09 18:57:35
![](https://img.aspxhome.com/file/2023/3/64983_0s.jpg)
TensorFlow安装及jupyter notebook配置方法
2022-09-11 01:05:23
未能加载文件或程序集“XXX”或它的某一个依赖项。试图加载格式不正确的程序。
2023-07-01 00:38:21
Python高阶函数与装饰器函数的深入讲解
2023-10-04 12:42:41
![](https://img.aspxhome.com/file/2023/7/63187_0s.png)
python查看自己安装的所有库并导出的命令
2022-03-01 03:03:08
![](https://img.aspxhome.com/file/2023/8/100938_0s.png)
Python实现的寻找前5个默尼森数算法示例
2023-05-15 17:34:29
详细介绍Python函数中的默认参数
2021-02-14 09:41:47
laravel实现简单用户权限的示例代码
2023-11-14 11:58:29
CSS Border使用小分享
2010-08-12 14:34:00
![](https://img.aspxhome.com/file/UploadPic/20108/12/box-model-47s.png)
asp实现本周的一周时间列表的代码
2011-04-06 10:45:00
JSP安全开发之XSS漏洞详解
2023-06-13 13:07:24
![](https://img.aspxhome.com/file/2023/1/62781_0s.png)
段正淳的css笔记(4)css代码的简写
2007-11-01 22:03:00
Python如何实现自带HTTP文件传输服务
2023-01-05 05:04:15
![](https://img.aspxhome.com/file/2023/9/90289_0s.png)
Python字符串中查找子串小技巧
2022-04-16 10:58:04
python中如何正确使用正则表达式的详细模式(Verbose mode expression)
2022-08-28 04:07:02
![](https://img.aspxhome.com/file/2023/3/79753_0s.png)
Tensorflow加载模型实现图像分类识别流程详解
2023-12-22 02:31:13
![](https://img.aspxhome.com/file/2023/7/107467_0s.jpg)
Python 的Json 模块编码详解
2023-03-19 08:29:15
微信小程序开发工具介绍
2023-08-10 23:05:40
![](https://img.aspxhome.com/file/2023/1/58651_0s.jpg)
JS实现点击下拉菜单把选择的内容同步到input输入框内的实例
2023-09-08 19:50:10
![](https://img.aspxhome.com/file/2023/0/56300_0s.jpg)