Python的条件锁与事件共享详解
作者:---dgw博客 时间:2021-12-16 22:02:23
1:事件机制共享队列:
利用消息机制在两个队列中,通过传递消息,实现可以控制的生产者消费者问题
要求:readthread读时,writethread不能写;writethread写时,readthread不能读。
基本方法 时间类(Event)
set:设置事件。将标志位设为True。
wait:等待事件。会将当前线程阻塞,直到标志位变为True。
clear:清除事件。将标志位设为False。
set() clear() 函数的交替执行 也就是消息传递的本质
模版:
基本code
# 事件消息机制
import queue
import threading
import random
from threading import Event
from threading import Thread
class WriteThread(Thread):
def __init__(self,q,wt,rt):
super().__init__();
self.queue=q;
self.rt=rt;
self.wt=wt;
def run(self):
self.rt.set()
self.wt.wait();
self.wt.clear();
class ReadThread(Thread):
def __init__(self,q,wt,rt):
super().__init__();
self.queue=q;
self.rt=rt;
self.wt=wt;
def run(self):
while True:
self.rt.wait();
self.wt.wait();
self.wt.clear()
参考代码:
# -*- coding: utf-8 -*-
"""
Created on Tue Sep 10 20:10:10 2019
@author: DGW-PC
"""
# 事件消息机制
import queue
import threading
import random
from threading import Event
from threading import Thread
class WriteThread(Thread):
def __init__(self,q,wt,rt):
super().__init__();
self.queue=q;
self.rt=rt;
self.wt=wt;
def run(self):
data=[random.randint(1,100) for _ in range(0,10)];
self.queue.put(data);
print("WriteThread写队列:",data);
self.rt.set(); # 发送读事件
print("WriteThread通知读");
print("WriteThread等待写");
self.wt.wait();
print("WriteThread收到写事件");
self.wt.clear();
class ReadThread(Thread):
def __init__(self,q,wt,rt):
super().__init__();
self.queue=q;
self.rt=rt;
self.wt=wt;
def run(self):
while True:
self.rt.wait();# 等待写事件 带来
print("ReadThread 收到读事件");
print("ReadThread 开始读{0}".format(self.queue.get()));
print("ReadThread 发送写事件");
self.wt.set();
self.rt.clear();
q=queue.Queue();
rt=Event();
wt=Event();
writethread=WriteThread(q,wt,rt); # 实例化对象的
readthread=ReadThread(q,wt,rt); # 实例化对象的
writethread.start();
readthread.start();
2:条件锁同步生产者消费者
作用: 在保护互斥资源的基础上,增加了条件判断的机制
即为使用wait() 函数 判断不满足当前条件的基础上,让当前线程的阻塞。
其他线程如果生成了满足了条件的资源 使用notify() notifyALl() 函数将刮起线程唤醒。
使用了 threading 的Condition 类
acquire() : 锁住当前资源
relarse() :释放当前锁住的资源
wait:挂起当前线程, 等待唤起 。
• notify:唤起被 wait 函数挂起的线程 。
• notif计All:唤起所有线程,防止线程永远处于沉默状态 。
模版:
基本code
from threading import Thread
from threading import Condition
import random
import time
lock=Condition(); # 声明条件锁
flag=0;
def cnsumer():
lock.acquire();
while flag==0:
lock.wait();
业务代码---
lock.relarse();
def product():
lock.acquire();
释放锁之前对控制变量进行操作,数据的操作控制 可以作为全局变量来锁定
lock.notifyALl();
lock.relarse();
参考代码code:
# -*- coding: utf-8 -*-
"""
Created on Wed Sep 11 21:40:41 2019
@author: DGW-PC
"""
# 条件锁生产者消费者
from threading import Thread
from threading import Condition
import random
import time
flag=0; # 声明控制标志
goods=0; # 事物表示
lock=Condition();
def consumer(x):
global flag;
global goods;
lock.acquire(); # 取得锁
while flag==0: # 便于多次进行消费
print("consumer %d进入等待" % x);
lock.wait();
print("consumer {0}:消费了{1}".format(x,goods));# format 次序从0开始
flag-=1;
lock.release(); #释放锁
def product(x):
global flag;
global goods;
time.sleep(3);
lock.acquire();
goods=random.randint(1,1000);
print("product {0} 产生了{1}".format(x,goods));
flag+=1;
lock.notifyAll();
lock.release();
threads=[];
for i in range(0,2):
t1=Thread(target=consumer,args=(i,));
t2=Thread(target=product,args=(i,));
t1.start();
t2.start();
threads.append(t1);
threads.append(t2);
for x in threads:
x.join();
来源:https://www.cnblogs.com/dgwblog/p/11509231.html
标签:python,条件,锁,事件,共享
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
php中使用key,value,current,next和prev函数遍历数组的方法
2023-10-18 20:17:39
举例讲解Python中metaclass元类的创建与使用
2023-12-11 23:06:57
![](https://img.aspxhome.com/file/2023/8/131148_0s.png)
Python的Flask框架中配置多个子域名的方法讲解
2021-03-06 19:46:32
Python基于jieba, wordcloud库生成中文词云
2022-12-24 06:31:04
![](https://img.aspxhome.com/file/2023/2/114522_0s.jpg)
python实现逆滤波与维纳滤波示例
2023-07-01 16:36:25
![](https://img.aspxhome.com/file/2023/0/65490_0s.jpg)
php字符串截取函数用法分析
2023-06-28 22:19:26
判断网页编码的方法python版
2022-06-29 10:01:18
使用httplib模块来制作Python下HTTP客户端的方法
2021-03-10 09:27:37
![](https://img.aspxhome.com/file/2023/1/110581_0s.jpg)
python使用Qt界面以及逻辑实现方法
2023-10-23 23:28:09
![](https://img.aspxhome.com/file/2023/4/87644_0s.png)
golang中defer的关键特性示例详解
2023-08-06 06:12:45
asp根据出生时间判断生肖
2008-03-24 19:49:00
详解python中的装饰器
2023-02-20 10:33:36
Python中用max()方法求最大值的介绍
2021-12-08 09:33:06
python实现连连看游戏
2021-12-02 00:25:10
PHP查询快递信息的方法
2023-11-14 10:25:48
jQuery中$.ajax的实现解析
2008-07-20 13:11:00
Python3实现转换Image图片格式
2021-06-06 21:04:25
python 用struct模块解决黏包问题
2023-08-18 02:13:20
通过Python 获取Android设备信息的轻量级框架
2021-08-18 21:41:00
ORACLE 报警日志如何查看?第1/2页
2009-07-02 12:06:00