zoukankan      html  css  js  c++  java
  • python中生成器的两段代码

    生产者-消费者经典单线程问题

    import time
    def consumer(name):
        print("%s 准备吃包子啦!" %name)
        while True:
           baozi = yield
     
           print("包子[%s]来了,被[%s]吃了!" %(baozi,name))
     
    def producer(name):
        c = consumer('A')
        c2 = consumer('B')
        c.__next__()
        c2.__next__()
        print("老子开始准备做包子啦!")
        for i in range(10):
            time.sleep(1)
            print("做了2个包子!")
            c.send(i)
            c2.send(i)
     
    producer("alex")
    执行加一的生成器:
    def gen(x):
        count = x
        while True:
            val = (yield count)
            if val is not None:
                count = val
            else:
                count += 1

    f = gen(5)
    print f.next()
    print f.next()
    print f.next()
    print '===================='
    print f.send(9)#发送数字9给生成器
    print f.next()
    print f.next()

  • 相关阅读:
    SQL注入漏洞
    回发或回调参数无效
    ListView绑定DataSet
    SQL注入漏洞全接触入门篇(二)
    条件运算符
    安全类
    ASP.NET常用代码集锦
    活动目录操作类
    x60系统安装步骤
    程序员是如何喝酒的
  • 原文地址:https://www.cnblogs.com/mandy-study/p/7718930.html
Copyright © 2011-2022 走看看