python yield
- 协程从语法上和生成器类似,都是定义中包含yield关键字函数
- 在协程中yield通常出现表达式的右边,如date=yield可以产出值,当然yield关键字后面没有表达式,那么生成产出None
- 协程可以把控制器让给中心调度程序,从而激活其他的协程
1.了解协程
-
一个简单例子
def coroutine(): print("start") x = yield print("end: ",x) coro = coroutine() next(coro) coro.send("886") """ start Traceback (most recent call last): end: 886 File "J:/flask_restful/ginger/test.py", line 36, in <module> coro.send("886") StopIteration """
对于伤处例子当我们通过next(...)激活协程后,程序会运行x = yield(这里需要知道x=yield是先计算等号右边的内容,然后赋值给x.所以激活生成器后,程序会阻塞在yield这里,但没有给x赋值),当调用send方法后yield或收到这个值并赋值给x,而当程序运行到协程定义体的末尾时,会抛出StopIteration异常。
-
如果协程没有通过next(...)激活,直接用send会报错。所以next(...)这一步预先激活协程,让协程向前执行到第一个yield,协程运行过程有四个状态:
- GEN_CREATE:等待开始执行
- GEN_RUNNING:解释器正在执行,这个状态一般看不到
- GEN_SUSPENDED:在yield表达式处暂停
- GEN_CLOSED:执行结束
>>> def coroutine(key): print("start:",key) key2 = yield key print("Received:",key2) key3 = yield key + key2 print("Received:",key3) >>> coro = coroutine(5) from inspect import getgeneratorstate print(getgeneratorstate(coro)) next(coro) print(getgeneratorstate(coro)) SyntaxError: multiple statements found while compiling a single statement >>> coro = coroutine(5) >>> from inspect import getgeneratorstate >>> getgeneratorstate(coro) 'GEN_CREATED' >>> next(coro) start: 5 5 >>> getgeneratorstate(coro) 'GEN_SUSPENDED' >>> coro.send(10) Received: 10 15 >>> coro.send(15) Received: 15 Traceback (most recent call last): File "<pyshell#9>", line 1, in <module> coro.send(15) StopIteration >>> getgeneratorstate(coro) 'GEN_CLOSED'
2.预激活装饰器演示
from functools import wraps
def coroutine(func):
@wraps(func)
def inner(*args,**kwargs):
gen = func(*args,**kwargs)
next(gen)
return gen
return inner
@coroutine
def averager():
total = 0
count = 0
average = None
while True:
term = yield average
total += term
count += 1
average = total/count
coro = averager()
from inspect import getgeneratorstate
print(getgeneratorstate(coro))
print(coro.send(10))
print(coro.send(20))
print(coro.send(30))
"""
GEN_SUSPENDED
10.0
15.0
20.0
"""
3.异常处理
- generator.throw会放生成器在yield表达式处抛出指定异常。如果生成器处理了抛出异常, 代码会向前执行到下一个yield表达式,而产出的值会成为调用generator.throw方法代码的返回值。如果生成器没有处理抛出的异常,异常会向上冒泡,传到调用方的上下文中。
def demo():
while True:
try:
x = yield
print(x)
except MyException:
print("My defind error")
exc = demo()
next(exc)
exc.send(10)
exc.send(20)
exc.throw(MyException)
exc.send(30)
"""
10
20
My defind error
30
"""
4.让协程返回值
- 获取写策划给你返回值
from collections import namedtuple
Result = namedtuple("Result","colunt average")
def averager():
total = 0.0
count = 0
average = None
while True:
term = yield
if term is None:
break
total += term
count+=1
average = total/count
return Result(count,average)
coro_avg = averager()
next(coro_avg)
coro_avg.send(10)
coro_avg.send(30)
coro_avg.send(5)
try:
coro_avg.send(None)
except StopIteration as e:
result = e.value
print(result)
"""
Result(colunt=3, average=15.0)
"""
-
这样获取返回值相对比较麻烦,而yield from 结构会自动不会StopIteration异常。这种储方式与for循环处理StopIteration异常方式一样。
def gen2(): yield from "Hi" yield from range(1,3) print(list(gen2())) """ ['H', 'i', 1, 2] """
- 通过yield from 不用自己处理异常。
from collections import namedtuple Result = namedtuple('Result', 'count average') # 子生成器 def averager(): total = 0.0 count = 0 average = None while True: term = yield if term is None: break total += term count += 1 average = total/count return Result(count, average) # 委派生成器 def grouper(result, key): while True: result[key] = yield from averager() # 客户端代码,即调用方 def main(data): results = {} for key,values in data.items(): group = grouper(results,key) next(group) for value in values: group.send(value) group.send(None) #这里表示要终止了 report(results) # 输出报告 def report(results): for key, result in sorted(results.items()): group, unit = key.split(';') print('{:2} {:5} averaging {:.2f}{}'.format( result.count, group, result.average, unit )) data = { 'girls;kg': [40.9, 38.5, 44.3, 42.2, 45.2, 41.7, 44.5, 38.0, 40.6, 44.5], 'girls;m': [1.6, 1.51, 1.4, 1.3, 1.41, 1.39, 1.33, 1.46, 1.45, 1.43], 'boys;kg': [39.0, 40.8, 43.2, 40.8, 43.1, 38.6, 41.4, 40.6, 36.3], 'boys;m': [1.38, 1.5, 1.32, 1.25, 1.37, 1.48, 1.25, 1.49, 1.46], } if __name__ == '__main__': main(data) 关于上述代码着重解释一下关于委派生成器部分,这里的循环每次迭代时会新建一个averager实例,每个实例都是作为协程使用的生成器对象。 grouper发送的每个值都会经由yield from处理,通过管道传给averager实例。grouper会在yield from表达式处暂停,等待averager实例处理客户端发来的值。averager实例运行完毕后,返回的值会绑定到results[key]上,while 循环会不断创建averager实例,处理更多的值 并且上述代码中的子生成器可以使用return 返回一个值,而返回的值会成为yield from表达式的值。