yield是个表达式而不仅仅是个语句,所以可以使用x = yield r 这样的语法, yield表达式可以接收send()发出的参数,yield表达式是跟send方法一起配合使用
send方法
next()和send()在一定意义上作用是相似的,区别是send()可以传递yield表达式的值进去,而next()不能传递特定的值,只能传递None进去。因此,我们可以看做c.next() 和 c.send(None) 作用是一样的。
def gen():
value=0
while True:
receive = yield value
if receive=='e':
break
value = 'got: %s' % receive
g=gen()
print(g.send(None))
print(g.send('aaa'))
print(g.send(3))
print(g.send('e'))
执行流程:
- 通过g.send(None)或者next(g)可以启动生成器函数,并执行到第一个yield语句结束的位置。此时,执行完了yield语句,但是没有给receive赋值。yield value会输出初始值0注意:在启动生成器函数时只能send(None),如果试图输入其它的值都会得到错误提示信息。
- 通过g.send(‘aaa’),会传入aaa,并赋值给receive,然后计算出value的值,并回到while头部,执行yield value语句有停止。此时yield value会输出”got: aaa”,然后挂起。
- 通过g.send(3),会重复第2步,最后输出结果为”got: 3″
- 当我们g.send(‘e’)时,程序会执行break然后推出循环,最后整个函数执行完毕,所以会得到StopIteration异常。
执行结果:
0
got: aaa
got: 3
Traceback (most recent call last):
File "C:\string.bak.py", line 20, in <module>
print(g.send('e'))
StopIteration
yield表达式实现协程
def consumer():
r = 'yield' while True: #当下边语句执行时,先执行yield r,然后consumer暂停,此时赋值运算还未进行 #等到producer调用send()时,send()的参数作为yield r表达式的值赋给等号左边 n = yield r #yield表达式可以接收send()发出的参数 if not n: return print('[CONSUMER] Consuming %s...' % n) r = '200 OK' def produce(c): c.send(None) #send需要先调用send(None),因为只有生成器是暂停状态才可以接收send的参数 n = 0 while n < 5: n = n + 1 print('[PRODUCER] Producing %s...' % n) r = c.send(n) #调用consumer生成器 print('[PRODUCER] Consumer return: %s' % r) c.close() c = consumer() produce(c)
理解send()恢复生成器的过程
def gen():
a = yield 1 print('yield a % s' % a) b = yield 2 print('yield b % s' % b) c = yield 3 print('yield c % s' % c) r = gen() x = next(r) print('next x %s' % x) y = r.send(10) print('next y %s' %y) z = next(r) print('next z %s' % z)
可以看到实际上y=r.send(10)
的参数10是赋给了a。整个运行过程即执行x=next(r)
之后,gen()执行了yield 1
然后暂停,没有进行对a的赋值。但再调用y=r.send(10)
时赋值过程继续,并把10赋给了a.