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

    import time
    def comsumer():
        need = input("请问需要多少包子:")
        return need
    def producer(n):
        res = []
        for i in range(1,n+1):
            time.sleep(0.2)
            res.append('包子%s' %i)
        return res
    while True:
        n = int(comsumer())
        m = producer(n)
        print(m,'
    这是您的%s个包子,请收好,欢迎下次光临' %n)

    代码结果如下:  

    请问需要多少包子:10
    ['包子1', '包子2', '包子3', '包子4', '包子5', '包子6', '包子7', '包子8', '包子9', '包子10']
    这是您的10个包子,请收好,欢迎下次光临
    请问需要多少包子:15
    ['包子1', '包子2', '包子3', '包子4', '包子5', '包子6', '包子7', '包子8', '包子9', '包子10', '包子11', '包子12', '包子13', '包子14', '包子15']
    这是您的15个包子,请收好,欢迎下次光临
    请问需要多少包子:

    import time
    def comsumer():
        while True:
            need = input()
            print("我需要%s个包子" %need)
            yield need
            baozi = yield
            print("谢谢您的%s个包子!" %baozi)
            producer()
    def producer():
        print("请问您需要多少包子?")
        a = comsumer()
        xuqiu = a.__next__()
        a.__next__()
        time.sleep(int(xuqiu)/10)
        res = ["包子%s" %i for i in range(1,int(xuqiu)+1)]
        print(res,"
    这是您的%s个包子,请收好,欢迎下次光临" %xuqiu)
        a.send(xuqiu)
    producer()

    代码结果如下:

    请问您需要多少包子?
    10
    我需要10个包子
    ['包子1', '包子2', '包子3', '包子4', '包子5', '包子6', '包子7', '包子8', '包子9', '包子10']
    这是您的10个包子,请收好,欢迎下次光临
    谢谢您的10个包子!
    请问您需要多少包子?

  • 相关阅读:
    PHP使用数据库永久连接方式操作MySQL的是与非
    php生成xml文件
    Ruby学习之类
    新增题目功能模块总结
    Ruby学习之类2
    smarty section循环成两列的问题
    jQuery validate插件初探
    Zend Framework学习之Zend_Config
    Zend Framework学习之Zend_Loader动态加载文件和类
    JS 删除字符串最后一个字符的方法
  • 原文地址:https://www.cnblogs.com/zhangsenzhen/p/9385244.html
Copyright © 2011-2022 走看看