zoukankan      html  css  js  c++  java
  • Python 生产者-消费者模型

    生产者-消费者模型是多线程同步的经典案例
    此模型中生产者向缓冲区push数据,消费者从缓冲区中pull数据
    这个Demo中缓冲区用python实现的Queue来做,这个模块是线程安全的使我不用再为队列增加额外的互斥锁.此外这个Demo中信号处理的实现是这样的:
    1)主线程接到一个SIGTERM的信号后先通知Consumer停止向缓冲区push数据并退出
    2)Produer将缓冲区中的数据消费完全后在退出
    3)主线程退出
    下面是部分代码,全部代码在github上面

    class Consumer(threading.Thread):
        def __init__(self, queue):
            threading.Thread.__init__(self)
            self.queue = queue
            self.do = True
    
        def stop(self):
            self.do = False
            print 'change consumer.do to False'
    
        def run(self):
            print 'Create new consumer thread, id: %s' % self.ident
            while self.do:
                messages = []
                result = []
                msg = random.randint(0,100)
                self.queue.put(msg)
            print 'Consumer thread will exit.'
    class Producer(threading.Thread):
        def __init__(self, queue):
            threading.Thread.__init__(self)
            self.queue = queue
            self.msgs = Queue.Queue()
            self.state = State.NORMAL
            self.do = True
    
        def stop(self):
            self.do = False
            self.state = State.STOP
    
        def run(self):
            while self.do:
                if self.state == State.NORMAL:
                    if not self.queue.empty():
                        data = self.queue.get()
                        print 'Producer get data: %s' % data
                    else:
                        print 'data queue is empty, sleep 5 seconds.'
                        time.sleep(5)
                elif self.state == State.STOP:
                    while not self.queue.empty():
                        data = self.queue.get()
                        print 'Producer get data: %s' % data
            print 'Producer thread will exit.'

    个人博客: http://www.yancey.info/?p=58 

  • 相关阅读:
    基于libevent的TLS单向认证CS通信验证
    ubuntu按照时间顺序列出apt安装的程序
    网页识别语音插件annyang可以实现识别中文
    微信小程序图片和签名
    linux run/media/wang/centos_磁盘爆满
    一个页面实现增删改查
    查某关键字在数据库中的哪个位置
    ADO.NET五大对象
    怎样获取当前时间
    string与stringbuilder的区别
  • 原文地址:https://www.cnblogs.com/yancey/p/3366577.html
Copyright © 2011-2022 走看看