python 线程间通信之Condition, Queue
Event
和 Condition
是threading模块原生提供的模块,原理简单,功能单一,它能发送 True
和 False
的指令,所以只能适用于某些简单的场景中。
而Queue
则是比较高级的模块,它可能发送任何类型的消息,包括字符串、字典等。其内部实现其实也引用了Condition
模块(譬如put
和get
函数的阻塞),正是其对Condition
进行了功能扩展,所以功能更加丰富,更能满足实际应用。
import threading, timeclass Hider(threading.Thread):def __init__(self, cond, name):super(Hider, self).__init__()self.cond = condself.name = namedef run(self):time.sleep(1) #确保先运行Seeker中的方法 self.cond.acquire()print(self.name + ': 我已经把眼睛蒙上了')self.cond.notify()self.cond.wait()print(self.name + ': 我找到你了哦 ~_~')self.cond.notify()self.cond.release()print(self.name + ': 我赢了')class Seeker(threading.Thread):def __init__(self, cond, name):super(Seeker, self).__init__()self.cond = condself.name = namedef run(self):self.cond.acquire()self.cond.wait()print(self.name + ': 我已经藏好了,你快来找我吧')self.cond.notify()self.cond.wait()self.cond.release()print(self.name + ': 被你找到了,哎~~~')cond = threading.Condition() seeker = Seeker(cond, 'seeker') hider = Hider(cond, 'hider') seeker.start() hider.start()from queue import Queue from threading import Thread import timeclass Student(Thread):def __init__(self, name, queue):super().__init__()self.name = nameself.queue = queuedef run(self):while True:# 阻塞程序,时刻监听老师,接收消息msg = self.queue.get()# 一旦发现点到自己名字,就赶紧答到if msg == self.name:print("{}:到!".format(self.name))class Teacher:def __init__(self, queue):self.queue=queuedef call(self, student_name):print("老师:{}来了没?".format(student_name))# 发送消息,要点谁的名 self.queue.put(student_name)queue = Queue() teacher = Teacher(queue=queue) s1 = Student(name="小明", queue=queue) s2 = Student(name="小亮", queue=queue) s1.start() s2.start()print('开始点名~') teacher.call('小明') time.sleep(1) teacher.call('小亮')
输出:
开始点名~ 老师:小明来了没? 小明:到! hider: 我已经把眼睛蒙上了 seeker: 我已经藏好了,你快来找我吧 hider: 我找到你了哦 ~_~ hider: 我赢了 seeker: 被你找到了,哎~~~ 老师:小亮来了没? 小亮:到!
转载于:https://www.cnblogs.com/aguncn/p/10179755.html
python 线程间通信之Condition, Queue相关推荐
- python线程间通信_python多线程之事件触发(线程间通信)
执行结果: 那么,通过分析执行结果来看,您已经体会到了其中的秘密.... 再脑补一下: Python提供了Event对象用于线程间通信,它是由线程设置的信号标志,如果信号标志位真,则其他线程等待直到信 ...
- python线程间通信方法之Event
最近写程序要用到线程间通信,在网上搜了些资料,测试了一下代码,在这里总结一下. Python实现线程间通信有几种方法,在这里介绍Event对象. Event对象实现了简单的线程通信机制,它提供了设置信 ...
- 线程间通信共享变量和queue
在多线程中,数据是共享,如何在多线程安全的通信,是首先要可虑的问题的 #线程间的通信 import timeimport threadingfrom threading import RLock de ...
- python线程间通信
""" 线程通信方法: 利用同步互斥机制(都是对共享资源的操作)多个线程对共享资源的操作代码称为临界区同步是一种协作关系,未完成操作,多进程或者线程之间形成一种协调,按照 ...
- python 线程通信 会涉及到拷贝吗_Python如何实现线程间通信
问题 你的程序中有多个线程,你需要在这些线程之间安全地交换信息或数据 解决方案 从一个线程向另一个线程发送数据最安全的方式可能就是使用 queue 库中的队列了.创建一个被多个线程共享的 Queue ...
- Python中的线程间通信
Python中的线程间通信 文章目录 Python中的线程间通信 1.Queue 2.同步机制 1.Event 2.Semaphore(信号量) 3.Lock(锁) 4.RLock(可重入锁) 5.C ...
- Python中线程间通信
Python中线程间通信 一.前言 二.什么是互斥锁 三.使用互斥锁 四.使用队列在线程间通信 五.关于线程需要注意的两点 一.前言 我们已经知道进程之间不能直接共享信息,那么线程之间可以共享信息 ...
- [Python]线程实例化;互斥锁;线程间通信
目录 1.使用threading创建线程 2.线程锁(互斥锁) 线程间通信 ----------到此,结束------------ 图穷匕见 1.使用threading创建线程 threading模块 ...
- python 线程通信的几种方式_进程间通信和线程间通信的几种方式
进程 进程(Process)是计算机中的程序关于某数据集合上的一次运行活动,是系统进行资源分配和调度的基本单位,是操作系统结构的基础.在早期面向进程设计的计算机结构中,进程是程序的基本执行实体:在当代 ...
最新文章
- oracle使用shutdown immediate前应做
- 「2017 山东一轮集训 Day2」Pair (霍尔定理+线段树)
- linux下php反编译apk,php反编译
- 腾讯视频下载安装免费装到手机_腾讯视频怎么上传个人本地视频
- python类与对象 动画_python之类与对象(2)
- linux mv命令源码,linux命令学习(8):mv命令
- java消除整型数组中重复的元素,排序后输出新数组
- 7.1立体环绕追踪音效|专属个性调音台,这副头戴电竞耳机有点料
- steam (游戏平台)
- 安装linux系统的pe软件,Linux和win系统制作U盘pe启动方法
- _stdcall 添加与否的作用
- 4. C语言预定义符号
- 人工蜂群算法求解货位优化问题
- 【数学建模】数学建模学习2---整数规划(例题+matlab代码实现)
- 如何成为一流的「匠人」?
- 河北官方:邯郸涉县致4死5伤煤气泄漏事故涉嫌瞒报
- 【PS插件】Retouch4me 10合1一键安装版
- C++程序员学习用C#写网站(四)
- 读书笔记 | 自动驾驶中的雷达信号处理(第6章 到达方向(DOA)估计算法 )
- 计算机设备管理主要内容,计算机设备管理方案范本.doc