zoukankan      html  css  js  c++  java
  • python基础一 day37 Queue

    # 队列
    # 生产者消费者模型

    # 生产者 进程
    # 消费者 进程
    import time
    import random
    from multiprocessing import Process,Queue
    def consumer(q,name):
    while True:
    food = q.get()
    if food is None:
    print('%s获取到了一个空'%name)
    break
    print('33[31m%s消费了%s33[0m' % (name,food))
    time.sleep(random.randint(1,3))

    def producer(name,food,q):
    for i in range(4):
    time.sleep(random.randint(1,3))
    f = '%s生产了%s%s'%(name,food,i)
    print(f)
    q.put(f)

    if __name__ == '__main__':
    q = Queue(20)
    p1 = Process(target=producer,args=('Egon','包子',q))
    p2 = Process(target=producer, args=('wusir','泔水', q))
    c1 = Process(target=consumer, args=(q,'alex'))
    c2 = Process(target=consumer, args=(q,'jinboss'))
    p1.start()
    p2.start()
    c1.start()
    c2.start()
    p1.join()
    p2.join()
    q.put(None)
    q.put(None)

  • 相关阅读:
    dbgrideh标题排序
    ctrl r w 去掉
    c# 中@ 的三种用法
    vs插件
    oracle查看会话(常规操作)
    3 docker容器
    k8s-组件
    k8s-常见错误
    k8s监控-kube-prometheus
    helm-私有仓库
  • 原文地址:https://www.cnblogs.com/wang-tan/p/11435549.html
Copyright © 2011-2022 走看看