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 ...
随机推荐
- socket发送请求,协程
1.socket发送请求 #发送请求的方式 #方式一 import requests ret = requests.get("https://www.baidu.com/s?wd=abc&q ...
- Android入门简介
GeoQuiz应用是由一个activity和一个布局(layout)组成. activity是Android SDK中Activity类的一个具体实例,负责管理用户与信息屏的交互. 布局定义了一系列用 ...
- linux初学者-mail篇
linux初学者-mail篇 邮件是在生活中比较常用的一个工具,在linux系统中的邮件也是.在linux中,邮件的发送所用的服务时postfix,邮件的接收所用的服务是pop(110端口).ima ...
- Linux设备驱动程序学习----2.内核模块与应用程序的对比
内核模块与应用程序的对比 更多内容请参考Linux设备驱动程序学习----目录 1. 内核模块与应用程序的对比 内核模块和应用程序之间的不同之处: 大多数中小规模的应用程序是从头到尾执行单个任务,而模 ...
- web-fragment模块化使用
用eclipse右键new->other->web->web fragment project 确定后修改dynamic web project name为你要输出到的项目,当然可以 ...
- oracle查询截至到当前日期月份所在年份的所有月份
SELECT to_number(TO_CHAR(add_months(trunc(sysdate, 'yy'), ROWNUM - 1), 'MM')) as month FROM DUALCONN ...
- Could not launch "APP_NAME" process launch failed: 4294967295
真机调试忽然遇到这个问题, Could not launch "APP_NAME" process launch failed: 如图所示: 模拟器上能正常调试………… 这个问题还 ...
- 关于报错:The Microsoft.ACE. Oledb.12.0 provider was not registered on the local computer
错误描述:The Microsoft.ACE. Oledb.12.0 provider was not registered on the local computer 最近在Web项目中做一个自动生 ...
- Docker Toolbox安装
公司最近搭建docker环境,其中会遇到一些问题,在这里记录一下. 先来了解一下docker 一.基本概念 1.Docker中基本概念镜像(Image) 提到镜像,有对操作系统有一定认知的都知道,镜像 ...
- 自定义SWT控件三之搜索功能下拉框
3.搜索功能下拉弹出框 package com.view.control.select; import java.util.ArrayList; import java.util.LinkedList ...