zoukankan      html  css  js  c++  java
  • python多线程编程(5): 队列同步

    前面介绍了互斥锁条件变量解决线程间的同步问题,并使用条件变量同步机制解决了生产者与消费者问题

    让我们考虑更复杂的一种场景:产品是各不相同的。这时只记录一个数量就不够了,还需要记录每个产品的细节。很容易想到需要用一个容器将这些产品记录下来。

    Python的Queue模块中提供了同步的、线程安全的队列类,包括FIFO(先入先出)队列Queue,LIFO(后入先出)队列LifoQueue,和优先级队列PriorityQueue。这些队列都实现了锁原语,能够在多线程中直接使用。可以使用队列来实现线程间的同步。

    用FIFO队列实现上述生产者与消费者问题的代码如下:

    #encoding=utf-8
    import threading
    import time
    from Queue import Queue
    
    class Producer(threading.Thread):
        def run(self):
            global queue
            count = 0
            while True:
                for i in range(100):
                    if queue.qsize() > 1000:
                         pass
                    else:
                         count = count +1
                         msg = '生成产品'+str(count)
                         queue.put(msg)
                         print msg
                time.sleep(1)
    
    class Consumer(threading.Thread):
        def run(self):
            global queue
            while True:
                for i in range(3):
                    if queue.qsize() < 100:
                        pass
                    else:
                        msg = self.name + '消费了 '+queue.get()
                        print msg
                time.sleep(1)
    
    queue = Queue()
    
    
    def test():
        for i in range(500):
            queue.put('初始产品'+str(i))
        for i in range(2):
            p = Producer()
            p.start()
        for i in range(5):
            c = Consumer()
            c.start()
    if __name__ == '__main__':
        test()
  • 相关阅读:
    Python 使用正则表达式匹配URL网址
    第3章 网络爬虫基础
    《精通Python网络爬虫》
    /etc/hosts
    Linux alias 命令
    file()
    Win10 取消桌面快捷键图标
    Win10 我的电脑 -- 右键点击管理打不开
    MongoDB 备份恢复
    ORACLE 日期比较
  • 原文地址:https://www.cnblogs.com/lczit/p/4435897.html
Copyright © 2011-2022 走看看