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();
6
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=;
def cnsumer():
lock.acquire();
while flag==:
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();

Python的条件锁与事件共享的更多相关文章

  1. python 并发编程 锁 / 信号量 / 事件 / 队列(进程间通信(IPC)) /生产者消费者模式

    (1)锁:进程之间数据不共享,但是共享同一套文件系统,所以访问同一个文件,或同一个打印终端,是没有问题的,而共享带来的是竞争,竞争带来的结果就是错乱,如何控制,就是加锁处理. 虽然使用加锁的形式实现了 ...

  2. python线程condition条件锁应用实例

    import time import threading # 吃火锅鱼丸 guo = [] suo = threading.Condition() #条件锁 # 生产者负责生产 class Produ ...

  3. python线程条件变量Condition(31)

    对于线程与线程之间的交互我们在前面的文章已经介绍了 python 互斥锁Lock / python事件Event , 今天继续介绍一种线程交互方式 – 线程条件变量Condition. 一.线程条件变 ...

  4. 第十五章、Python多线程同步锁,死锁和递归锁

    目录 第十五章.Python多线程同步锁,死锁和递归锁 1. 引子: 2.同步锁 3.死锁 引子: 4.递归锁RLock 原理: 不多说,放代码 总结: 5. 大总结 第十五章.Python多线程同步 ...

  5. 【python】多进程锁multiprocess.Lock

    [python]多进程锁multiprocess.Lock 2013-09-13 13:48 11613人阅读 评论(2) 收藏 举报  分类: Python(38)  同步的方法基本与多线程相同. ...

  6. 并发编程---线程 ;python中各种锁

    一,概念 在传统操作系统中,每个进程有一个地址空间,而且默认就有一个控制线程 线程顾名思义,就是一条流水线工作的过程,一条流水线必须属于一个车间,一个车间的工作过程是一个进程 --车间负责把资源整合到 ...

  7. 孤荷凌寒自学python第四十天python 的线程锁RLock

     孤荷凌寒自学python第四十天python的线程锁RLock (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 因为研究同时在多线程中读写同一个文本文件引发冲突,所以使用Lock锁尝试同步, ...

  8. 孤荷凌寒自学python第三十九天python 的线程锁Lock

    孤荷凌寒自学python第三十九天python的线程锁Lock (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 当多个线程同时操作一个文件等需要同时操作某一对象的情况发生时,很有可能发生冲突, ...

  9. Python的互斥锁与信号量

    并发与锁 a. 多个线程共享数据的时候,如果数据不进行保护,那么可能出现数据不一致现象,使用锁,信号量.条件锁 b. c.互斥锁1. 互斥锁,是使用一把锁把代码保护起来,以牺牲性能换取代码的安全性,那 ...

随机推荐

  1. 官方版vs2008至vs2013下载地址

    Visual Studio 2005 Professional 官方90天试用版 英文版:http://download.microsoft.com/download/e/0/4/e04de840-8 ...

  2. 不能绑定到端口号:9194原因:Cannot assign requested address: JVM_Bind

    晚上将老服务器程序从win2008部署在新的云服务器win2012上,其实就是复制过去改改配置,启动时突然报不能绑定到端口号:9194原因:Cannot assign requested addres ...

  3. 逆向学习周记-C语言空函数

    实验环境:WIN7虚拟机 软件:VC6 首先在VC6里面写一个空函数Fun(): F7编译运行一下,没有出错,接着在函数处使用F9下断点,使程序运行到Fun函数时停下. 接着F5开始运行这个程序 程序 ...

  4. vue-property-decorator和typescript结合构建的class类组件,父组件触发子组件方法的方式

    vue-property-decorator和typescript结合构建的class类组件,父组件触发子组件方法的方式 class类组件示例 Father类组件 <template> & ...

  5. IT兄弟连 HTML5教程 HTML5表单 HTML5新增表单元素

    HTML5有一些新的表单元素:<datalist>.<keygen>.<output>.不是所有的浏览器都支持HTML5新的表单元素,但即使浏览器不支持该表单属性, ...

  6. Win10(64位)安装汇编环境(MASM)

    1:需要的文件 需要的安装包:这些百度都能下载找到 1).DOSBox 链接: 2) .MASM5.0 链接: 3).DEBUG 链接: 下面给出我们打包的环境 直接可用: (汇编我并不需要关注安装这 ...

  7. SpringCloud的入门学习之概念理解、Feign负载均衡入门

    1.Feign是SpringCloud的一个负载均衡组件. Feign是一个声明式WebService客户端.使用Feign能让编写Web Service客户端更加简单, 它的使用方法是定义一个接口, ...

  8. python distutils 基本打包与发布

    distutils 实现对package 包的发布 import math def showMsg(a): return a * a * a a = 10 print('%d 的三次方是 %d' % ...

  9. 用 Python 实现植物大战僵尸代码!

    前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者: marble_xu GitHub地址:https://github ...

  10. Scrapy框架-爬虫程序相关属性和方法汇总

    一.爬虫项目类相关属性 name:爬虫任务的名称 allowed_domains:允许访问的网站 start_urls: 如果没有指定url,就从该列表中读取url来生成第一个请求 custom_se ...