zoukankan      html  css  js  c++  java
  • day09_11 queue-生产者消费者

    
    __author__ = "Alex Li"
    
    import threading, time
    import queue
    
    '''
    在并发编程中使用生产者和消费者模式能够解决绝大多数并发问题。
    该模式通过平衡生产线程和消费线程的工作能力来提高程序的整体处理数据的速度。
    '''
    
    q = queue.Queue(maxsize=10)
    
    
    def producer(name):
    count = 1
    while True:
    q.put("骨头%s" % count)
    print("生产了骨头", count)
    count += 1
    time.sleep(0.1)
    
    
    def consumer(name):
    # while q.qsize() > 0:
    while True:
    print("[%s] 取到[%s] 并且吃了它..." % (name, q.get()))
    time.sleep(1)
    
    
    p = threading.Thread(target=producer, args=("Alex",))
    
    # 去队列取值,不会乱序,平衡生产线程和消费线程的工作能力
    c = threading.Thread(target=consumer, args=("ChengRonghua",))
    c1 = threading.Thread(target=consumer, args=("王森",))
    
    p.start()
    c.start()
    c1.start()
    
    
  • 相关阅读:
    PHP和Ajax设置页面请求超时
    Flex 布局教程
    数据库访问优化法则
    phpcms网站搬家至服务器
    phpcms网页替换验证码及搜索功能
    php判断手机段登录
    php环境搭建
    ThinkPHP框架
    JQuery事件
    JQuery
  • 原文地址:https://www.cnblogs.com/netflix/p/14855393.html
Copyright © 2011-2022 走看看