消息队列 Queue模块

#multiprocessing模块提供了Queue类 和 Pipe函数 实现消息队列
#消息队列
#
!/usr/bin/env python from multiprocessing import Process, Queue def producer(q): for i in xrange(5): q.put(i) print 'put {0} into queue'.format(i) def consumer(q): while 1: result = q.get() print 'get {0} from queue'.format(result) if q.empty(): break if __name__ == '__main__': q = Queue() p = Process(target=producer, args=(q,)) c = Process(target=consumer, args=(q,)) p.start() p.join() c.start()

 

Pipe方法返回一个二元元组(conn1, conn2),两个元素分别是两个连接对象,代表管道的两端,Pipe(duplex=True) 函数有一个默认参数duplex,默认等于True,表示这个管道是全双工模式,也就是说conn1和conn2均可收发;如果duplex=False,那么conn2只负责发消息到消息队列,conn1只负责从消息队列中读取消息

连接对象的常用方法有三个:
  • send()   ---> 发送消息到管道
  • recv()   ---> 从管道中读取消息
  • close()   --->关闭管道
#!/usr/bin/env python

import time
from multiprocessing import Pipe, Process

def producer(p):
    for i in xrange(5):
        p.send(i)
        print 'send {0} to pipe'.format(i)
        time.sleep(1)

def consumer(p):
    n = 5
    while n>0:
        result = p.recv()
        print 'recv {0} from pipe'.format(result)
        n -= 1

if __name__ == '__main__':
    p = Pipe(duplex=False)
    print p
    p1 = Process(target=producer, args=(p[1],))
    p2 = Process(target=consumer, args=(p[0],))
    p1.start()
    p2.start()
    p1.join()
    p2.join()
    p[0].close()
    p[1].close()

 

#!/usr/bin/env python
# encoding: utf-8

"""
@author: xyt
@file: queue.py
@time: 2017/12/18 21:35
"""
import Queue
from threading import Thread

import time


class Producer(Thread):
    def __init__(self,queue):
        super(Producer, self).__init__()
        self.queue = queue
    def run(self):
        for i in xrange(1,11):
            print("put data {0} in queue!")
            self.queue.put(i)

class Consumer(Thread):
    def __init__(self,queue):
        super(Consumer, self).__init__()
        self.queue = queue
    def run(self):
        while not self.queue.empty():
            number = self.queue.get()
            print("The data {0} from queue!".format(number))

def main():
    queue = Queue.Queue()
    p = Producer(queue)
    p.start()
    p.join()
    time.sleep(1)
    c = Consumer(queue)
    c.start()
    c.join()
    print("Main end!")
if __name__ == '__main__':
    main()

 

posted @ 2017-12-16 11:59  木头爱木头媳妇  阅读(223)  评论(0)    收藏  举报