python 实现多个线程间消息队列传递,一个简单的列子
- #-*-coding:utf8-*-
"""
Producer and consumer models:
1. There are many producers and consumers at the same time, but they complement each other.- Implemented by message queuing to achieve at the same time production and consumpion processing.
- """
- import threading
import time
import Queue
import random- lock = threading.RLock()
q = Queue.Queue()
count = 0- #第一个线程,用于入队列
class Mythread_1(threading.Thread):- def __init__(self,Producername):
threading.Thread.__init__(self)
self.Producername = Producername- def Producer(self,name):
- name = self.Producername
- for i in range(20):
#进队列
q.put(i)
print '\033[34;1mProducer %s,now produce %s to the consumers...\033[0m' %(name,i)
#通过控制休眠,来观察入列情况
time.sleep(random.randrange(2))
print 'Producer comes here'- def run(self):
#lock.acquire()
self.Producer(self.Producername)
#lock.release()- #第二个线程用于出队列
class Mythread_2(threading.Thread):
def __init__(self,Consumername):
threading.Thread.__init__(self)
self.Consumername = Consumername- def Consumer(self,name):
- name = self.Consumername
global count- while count < 20:
#lock.acquire()
#出队列
data = q.get()- print '\033[35;1mConsumer %s,now get the %s from the producers...\033[0m' %(name,data)
count += 1
#用来控制休眠之间,以便来观察出队列情况
time.sleep(random.randrange(2))
print 'Consumer comes here'- def run(self):
#lock.acquire()
self.Consumer(self.Consumername)
#lock.release()- t1 = Mythread_1("longyongzhen")
- t2 = Mythread_2("weichunyuan")
- '''
def Producer(name):
for i in range(20):
q.put(i)
print '\033[34;1mProducer %s,now produce %s to the consumers...\033[0m' %(name,i)
time.sleep(random.randrange(3))- def Consumer(name):
count = 0
while count < 20:
data = q.get()
print '\033[35;1mConsumer %s,now get the %s from the producers...\033[0m' %(name,data)
count += 1
time.sleep(random.randrange(2))- p = threading.Thread(target=Producer,args=("longyongzhen",))
c = threading.Thread(target=Consumer,args=("weichunyuan",))- p.start()
c.start()
'''
t1.start()
t2.start()
-----------------------------------
多次实验可以得出的结果是:
1、python的消息队列只要定义一个队列,如上面的q,则不同线程之间队列的入列和出列,只要调用q,则保证了队列的一致性,入列出列是对应的。
2、如果要对线程进行加锁,则队列的出列,要等所有都入列了之后才会释放资源,这种方式资源利用率太低。
python 实现多个线程间消息队列传递,一个简单的列子的更多相关文章
- 深入浅出Win32多线程设计之MFC的多线程-线程与消息队列(经典)
1.创建和终止线程 在MFC程序中创建一个线程,宜调用AfxBeginThread函数.该函数因参数不同而具有两种重载版本,分别对应工作者线程和用户接口(UI)线程. 工作者线程 CWinThread ...
- python多进程之间的通信:消息队列Queue
python中进程的通信:消息队列. 我们知道进程是互相独立的,各自运行在自己独立的内存空间. 所以进程之间不共享任何变量. 我们要想进程之间互相通信,传送一些东西怎么办? 需要用到消息队列!! 进程 ...
- Windows 消息以及消息处理算法--线程和消息队列详解
Windows以消息驱动的方式,使得线程能够通过处理消息来响应外界. Windows 为每个需要接受消息和处理消息的线程建立消息队列(包括发送消息队列,登记消息队列,输入消息队列,响应消息队列),其中 ...
- 在Windows系统上实现轻量级的线程间及进程间消息队列
Windows没有message queue累世的IPC内核对象,使得在在处理IPC时少了一种传递消息的手段. 利用Windows的Naming Object可以实现一套简单的Inter-Thread ...
- python【第十一篇】消息队列RabbitMQ、缓存数据库Redis
大纲 1.RabbitMQ 2.Redis 1.RabbitMQ消息队列 1.1 RabbitMQ简介 AMQP,即Advanced Message Queuing Protocol,高级消息队列协议 ...
- android消息线程和消息队列
基于消息队列的线程通信: 消息队列与线程循环 MessageQueue: 利用链表来管理消息. Mess ...
- linux 进程间消息队列通讯
转自:http://blog.csdn.net/lifan5/article/details/7588529 http://www.cnblogs.com/kunhu/p/3608589.html 前 ...
- 消息队列 ActiveMQ的简单了解以及点对点与发布订阅的方法实现ActiveMQ
Apache ActiveMQ是Apache软件基金会所研发的开放源代码消息中间件: 由于ActiveMQ是一个纯Java程序,因此只需要操作系统支持Java虚拟机,ActiveMQ便可执行. Act ...
- 消息队列之ActiveMQ简单环境搭建
准备: 环境:win7,Eclipse,jdk1.8 ActiveMQ版本:ActiveMQ 5.9.0 Release下载地址:http://activemq.apache.org/download ...
随机推荐
- Python连载25-函数tell&write&writeline$&持久化
一. 1.连续打印举例 #打开文件,三个字符一组读出来内容,然后显示在屏幕上,每读一次,停一秒 import time with open(r"test01.txt",'r') a ...
- K2 smarforms 控件整理
K2 Community – Market – Smatform Controls l K2 blackpearl 1. Drag and Drop Upload Control http: ...
- ThinkPHP5.0 模板
ThinkPHP5.0 模板 模板渲染 默认的视图目录是默认的模块下的view目录 渲染规则:调用 \think\View 类fetch方法 // [模板文件目录]/当前控制器名(小写+下划线)/当前 ...
- java练习---3
//程序员:罗元昊 2017.9.6public class World{ public static void main(String[] args){ double p=3.14,i=5.50; ...
- 第二章 jQuery框架使用准备
window常用属性: History:有关客户访问过的URL的信息 Location: 有关当前url的信息 常用方法: Confirm()将弹出一个确认对话框 open()在页面上弹出一个新的浏览 ...
- Jmeter脚本录制--HTTP代理服务器
Jmeter脚本录制功能依赖第三方工具Badboy,所以在安装了Jmeter之后,还需要再安装一个工具. Badboy本身自带浏览器,相关操作只能在Badboy上进行操作,偶尔可能会遇到浏览器兼容的问 ...
- win10下nodejs的安装及配置
这里主要引用两篇文章,写的非常详细,也能解决你可能出现的问题 nodejs安装及配置 如何删除之前nodejs设置的 npm config set prefix .....
- 【eclipse】No enclosing instance of type A is accessible. Must qualify the allocation with an enclosing instance of type A
用 eclipse 写 Java 代码时出现了这个问题,详细如下: No enclosing instance of type TestParsingLinkedList is accessible. ...
- K8S 部署 Web UI
在早期的版本中 Kubernetes可以在 Dashboard 中看到 heapster 提供的一些图表信息, 在后续的版本中会陆续移除掉 heapster,现在更加流行的监控工具是 promethe ...
- 【OpenCV-ANN神经网络自动驾驶】树莓派OpenCV神经网络自动驾驶小车【源码+实物】
没错!这个是我的毕业设计!!! 整个电子信息学院唯一一个优秀毕业设计 拿到这里炫耀了 实物如下: 电脑端显示效果: 自动驾驶实现过程: 1. 收集图像数据.建立局域网,让主机和Raspberry Pi ...