生产者消费者yield关键字与send方法的理解

# -*- coding:utf-8 -*-
from __future__ import unicode_literals

#例子1:
def test():
print('开始啦')
first = yield
print('first'first)
yield 2
print('second')


if __name__ == '__main__':
t = test()
res = t.next()
print(res)
res = t.send('函数停留在first那个位置,我就是给first赋值的')
print(res)
#例子2:
 
# -*- coding:utf-8 -*-
from __future__ import unicode_literals
import time


def consumer3(name):
print("我是%s,我准备吃包子了% name)
while True:
baozi = yield
time.sleep(1)
print("我是%s,我开始吃%s% (namebaozi))


def productor3():
c1 = consumer3("wupeiqi")
c2 = consumer3("csb")
c1.next()
c2.next()
for in range(10):
time.sleep(1)
c1.send("包子%s" %n)
c2.send("包面%s" %n)


if __name__ == '__main__':
productor3()
#例子3:
def consumer2(name):
print('我是%s,我准备开始吃包子了%name)
while True:
baozi = yield
print('我是%s,我很开心地把%s包子吃掉了%(namebaozi))


if __name__ == '__main__':
c1 = consumer2("wupeiqi")
c1.next()
c1.send("屎尿包子")

猜你喜欢

转载自blog.csdn.net/qq_28286027/article/details/81488532