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个包子!
    请问您需要多少包子?

  • 相关阅读:
    ABCD 谁是小偷
    三剑客-awk(简写)
    三剑客-sed(简写)
    Mysql锁
    MySQL两种内核对比
    netty源码分析(一)
    netty源码分析(二)
    网络编程之NIO
    网络编程之ByteBuffer
    线程池的原码分析(二)
  • 原文地址:https://www.cnblogs.com/zhangsenzhen/p/9385244.html
Copyright © 2011-2022 走看看