zoukankan      html  css  js  c++  java
  • 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 '33[34;1mProducer %s,now produce %s to the consumers...33[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 '33[35;1mConsumer %s,now get the %s from the producers...33[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 '33[34;1mProducer %s,now produce %s to the consumers...33[0m' %(name,i)
    time.sleep(random.randrange(3))

    def Consumer(name):
    count = 0
    while count < 20:
    data = q.get()
    print '33[35;1mConsumer %s,now get the %s from the producers...33[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、如果要对线程进行加锁,则队列的出列,要等所有都入列了之后才会释放资源,这种方式资源利用率太低。



  • 相关阅读:
    链表补充及链表和数组的区别
    单链表(不带头结点)
    动态数组
    【笔记】SQL语言的设计与编写
    netty 学习笔记一:感受 IO编程 NIO编程 与 Netty 编程
    分享系列——Thread#join() 在 Java 源码中并没有 notify ,被阻塞线程是如何唤醒的?答案在 JVM
    RabbitMQ 安装 图笔记版
    RabbitMQ 安装——RPM 和 TAR 两种方式
    学原理脱坑 之 centos 6/7 更新 UTC 以及 设置系统时区
    数据结构和算法篇——散列表
  • 原文地址:https://www.cnblogs.com/longyongzhen/p/7500778.html
Copyright © 2011-2022 走看看