Eventlet实现简单的生产者-消费者模型

2020-04-08  本文已影响0人  我要牛肉面面

问题如下:

编写三个线程,线程1每秒生产1件产品放入队列,线程2每秒生产2件产品放入队列,线程3每秒从队列中消费若干件产品,数量在1--5中随机取值。队列有容量限制,队列满时,生产者(线程1和线程2)等待,消费者(线程3)不受影响;队列空时,消费者等待,生产者不受影响。

我对线程不熟,这里用Python2.7+Eventlet的协程实现一下,算是证明一下自己做过这个东西

#!/usr/bin/python

import eventlet
import eventlet.queue as Queue
import random

QUEUE_LENGTH = 10

queue = Queue.Queue(QUEUE_LENGTH)

def new_product():
    return random.randint(0, 1000)

class Producer():
    def __init__(self, rate):
        self.rate = rate
        self.inverse_rate = 1.0 / self.rate
    
    def run(self):
        while True:
            if queue.full():
                print "Producer: queue full"
            else:
                prod = new_product()
                queue.put(prod)
                print "Producer:", prod
            eventlet.sleep(self.inverse_rate)

class Consumer():
    def __init__(self):
        pass
    
    def run(self):
        while True:
            for i in range(random.randint(1, 5)):
                if queue.empty():
                    print "Consumer: queue empty"
                    break
                else:
                    data = queue.get()
                    print "Consumer:", data
            eventlet.sleep(1)

def main():
    print "Monitor: Started"
    try:
        job_instances = [Producer(1),
                         Producer(2),
                         Consumer()
                        ]
        greenthreads = [eventlet.spawn(job.run) for job in job_instances]
        print "Monitor: all greenthreads spawned"
        for gt in greenthreads:
            gt.wait()
        print "Monitor: all greenthreads finished"
    except (KeyboardInterrupt, SystemExit), e:
        print "Monitor: Killed"

if __name__ == '__main__':
    main()

Eventlet据我所知不支持Python3,所以之前还看了一点Python3>=3.4里新增的AsyncIO,正好手里有个私人项目准备用协程+生产者消费者模型。然而由于个人和家庭原因,这个项目卡在设计阶段已经太久了,一直没动手写。

上一篇下一篇

猜你喜欢

热点阅读