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

    import time
    def consumer(name):
    print ("%s consumer one product" %name)
    while True:
    baozi = yield
    print ("baozi[%s] bei [%s] chile" %(baozi,name))

    def producter(name):
    c = consumer('A')
    c2 = consumer('B')
    c.__next__()
    c2.__next__()
    print ("begin to make baozi")
    for i in range(10):
    time.sleep(1)
    print ("make two baozi")
    c.send(i)
    c2.send(i)

    producter('alex')

    结果:

    A consumer one product
    B consumer one product
    begin to make baozi
    make two baozi
    baozi[0] bei [A] chile
    baozi[0] bei [B] chile
    make two baozi
    baozi[1] bei [A] chile
    baozi[1] bei [B] chile
    make two baozi
    baozi[2] bei [A] chile
    baozi[2] bei [B] chile
    make two baozi
    baozi[3] bei [A] chile
    baozi[3] bei [B] chile
    make two baozi
    baozi[4] bei [A] chile
    baozi[4] bei [B] chile
    make two baozi
    baozi[5] bei [A] chile
    baozi[5] bei [B] chile
    make two baozi
    baozi[6] bei [A] chile
    baozi[6] bei [B] chile
    make two baozi
    baozi[7] bei [A] chile
    baozi[7] bei [B] chile
    make two baozi
    baozi[8] bei [A] chile
    baozi[8] bei [B] chile
    make two baozi
    baozi[9] bei [A] chile
    baozi[9] bei [B] chile

     结论:c,c2返回为迭代器,故而consumer为生产者,注意用断点调试的方法来查看。

  • 相关阅读:
    SQL语句集(转)
    Oracle 数据库 for update / for update nowait 的区别
    XML 关键字
    JAVA 操作 DBF 文件数据库
    Hibernate 懒加载和 Json 序列化冲突
    MYSQL 语法大全自己总结的
    php-laravel中间件使用
    php-表单验证
    php-laravel安装与使用
    iOS 关于权限设置的问题
  • 原文地址:https://www.cnblogs.com/python-study/p/5463681.html
Copyright © 2011-2022 走看看