#-*-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、如果要对线程进行加锁,则队列的出列,要等所有都入列了之后才会释放资源,这种方式资源利用率太低。

最新文章

  1. ASP.NET Core中的ActionFilter与DI
  2. 从汇编角度分析C语言的过程调用
  3. Generate Time Data(财务日期主数据)
  4. Python基础(二)之字符串
  5. 使用 PHPMailer 发送邮件
  6. C语言语句分类:大致可分为六大类
  7. Arrays.asList(数组) 解说
  8. ExtJs之进度条实现
  9. js基础第七天
  10. Vim识别编码
  11. HTML5学习参考资料整理
  12. Linux 相关的error处理
  13. bzoj:1692 [Usaco2007 Dec]队列变换&amp;&amp;1640 [Usaco2007 Nov]Best Cow Line 队列变换
  14. @requestBody注解
  15. C# 网络爬虫利器之Html Agility Pack如何快速实现解析Html
  16. Java IO--NIO(一)
  17. XQuery:查询任何可作为 XML 形态呈现的数据,包括数据库
  18. mysql,存储引擎,事务,锁,慢查询,执行计划分析,sql优化
  19. DeveloperAppleHelp
  20. 【10】Quartz.net 定时服务实例

热门文章

  1. Apache Tomcat 绿色版安装Service(服务)
  2. laravel5.6 使用迁移创建表
  3. 利用 ssh 传输文件
  4. GDB 基本用法
  5. Git/Github使用方法小记
  6. 关于 &#39;&lt;a[^&gt;]+href=[&quot;\&#39;](.*?)[&quot;\&#39;]&#39; 的解释
  7. html以前没有学到的标签
  8. kubeadm定制化开发,延长证书
  9. C#实现简单爬虫
  10. java根据经纬度查询门店地理位置-完美解决附近门店问题