#-*-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 实现多个线程间消息队列传递,一个简单的列子的更多相关文章

  1. 深入浅出Win32多线程设计之MFC的多线程-线程与消息队列(经典)

    1.创建和终止线程 在MFC程序中创建一个线程,宜调用AfxBeginThread函数.该函数因参数不同而具有两种重载版本,分别对应工作者线程和用户接口(UI)线程. 工作者线程 CWinThread ...

  2. python多进程之间的通信:消息队列Queue

    python中进程的通信:消息队列. 我们知道进程是互相独立的,各自运行在自己独立的内存空间. 所以进程之间不共享任何变量. 我们要想进程之间互相通信,传送一些东西怎么办? 需要用到消息队列!! 进程 ...

  3. Windows 消息以及消息处理算法--线程和消息队列详解

    Windows以消息驱动的方式,使得线程能够通过处理消息来响应外界. Windows 为每个需要接受消息和处理消息的线程建立消息队列(包括发送消息队列,登记消息队列,输入消息队列,响应消息队列),其中 ...

  4. 在Windows系统上实现轻量级的线程间及进程间消息队列

    Windows没有message queue累世的IPC内核对象,使得在在处理IPC时少了一种传递消息的手段. 利用Windows的Naming Object可以实现一套简单的Inter-Thread ...

  5. python【第十一篇】消息队列RabbitMQ、缓存数据库Redis

    大纲 1.RabbitMQ 2.Redis 1.RabbitMQ消息队列 1.1 RabbitMQ简介 AMQP,即Advanced Message Queuing Protocol,高级消息队列协议 ...

  6. android消息线程和消息队列

    基于消息队列的线程通信:           消息队列与线程循环            MessageQueue:           利用链表来管理消息.                  Mess ...

  7. linux 进程间消息队列通讯

    转自:http://blog.csdn.net/lifan5/article/details/7588529 http://www.cnblogs.com/kunhu/p/3608589.html 前 ...

  8. 消息队列 ActiveMQ的简单了解以及点对点与发布订阅的方法实现ActiveMQ

    Apache ActiveMQ是Apache软件基金会所研发的开放源代码消息中间件: 由于ActiveMQ是一个纯Java程序,因此只需要操作系统支持Java虚拟机,ActiveMQ便可执行. Act ...

  9. 消息队列之ActiveMQ简单环境搭建

    准备: 环境:win7,Eclipse,jdk1.8 ActiveMQ版本:ActiveMQ 5.9.0 Release下载地址:http://activemq.apache.org/download ...

随机推荐

  1. [leetcode] 263. Ugly Number (easy)

    只要存在一种因数分解后,其因子是2,3,5中的一种或多种,就算是ugly数字. 思路: 以2/3/5作为除数除后,最后结果等于1的就是ugly数字 Runtime: 4 ms, faster than ...

  2. [leetcode] 21. Merge Two Sorted Lists (Easy)

    合并链表 Runtime: 4 ms, faster than 100.00% of C++ online submissions for Merge Two Sorted Lists. class ...

  3. 一文了解JVM

    一.什么是JVM JVM是Java Virtual Machine(Java 虚拟机)的缩写,JVM是一种用于计算设备的规范,它是一个虚构出来的计算机,是通过在实际的计算机上仿真模拟各种计算机功能来实 ...

  4. Cesium 学习(三)各种资源链接

    1.前言 前面已经介绍如何获得以及安装部署Cesium,接下来分享下学习资源链接,其中访问最多的是官网以及超图.火星的demo网站. 2.官网地址及介绍 官网:https://cesiumjs.org ...

  5. 用python输出菱形

    num = eval(input('请输入最多*所在行数:')) a = num b = num #上三角 for i in range(1,num+1): print((a-1) *' ', (2* ...

  6. junit的Test不能使用,报错信息:Test is not an annotation type

    在使用junit的Test做测试时,注解@Test报错”Test is not an annotation type”,发现是因为测试类的类名命名为了Test,所以导致错误. 测试类类名不能直接命名为 ...

  7. .net持续集成sonarqube篇之 sonarqube触发webhook

    系列目录 WebHook近些年来变得越来越流行,github,gitlab等代码托管平台都提供webhook功能.关于webhook这里不做详细介绍,大家可以参阅读相关互联网书籍或者材料来更深了解.可 ...

  8. python 实现两个文本文件内容去重

    实现两个文本内容去重,输出两个文本不重复的结果 两个测试文本内容如下 1.txt中内容为 1 2 3 4 5 6 7 8 2.txt中内容为 1 2 3 4 5 6 7 8 9 10 11 12 13 ...

  9. 【Python】Django【邮箱验证】 后端验证如何生成 token加密验证码 与如何解码!!!!

    1.生成token验证码方案   ,使用itsdangerous    大宝剑, 可以序列化出验证码,并能设置过期时间 安装 itsdangerous pip install itsdangerous ...

  10. 补充Java面试记录

    补充Java面试记录 背景:这两天面试遇到的部分问题都分散在了前面两篇文摘中,这里再做一些其他的记录,以备不时之需! 一.谈谈你对SpringBoot的理解? SpringBoot简介:SpringB ...