使用yield实现单线程中的异步并发效果

import time

def consumer(name):

   print("%s  准备 吃包子“%name)

   while Ture:

     baozi=yield--------------------------send 传的i

     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("zy")

原文地址:https://www.cnblogs.com/my334420/p/6385866.html