Python 协程 - Coroutines

 1 协程 - Coroutines
 2 
 3 Awaitable Objects,
 4     Awaitable Objects 通常由 __await__() 生成, 而
 5     Coroutine objects 是由 async def 关键字定义的函数 return 的 Awaitable Objects.
 6     Note,
 7         被 types.coroutine() or asyncio.coroutine() 装饰的生成器迭代器对象(generator iterator objects)
 8         是没有通过 __await__() 生成的 Awaitable Objects.
 9 
10     object.__await__(self) 必须 return 一个 iterator, 用来生成 Awaitable Objects.
11     如, asyncio.Future 执行这个方法一兼容 await 表达式.
12     See also PEP 492 for additional information about awaitable objects.
13 
14 Coroutine Objects,
15     Coroutine objects 是 awaitable objects, 是一个 __await__() return 的 iterator,
16     Coroutine 的执行就是对 __await__() return 的 iterator 的迭代. 跟 iterator 一样,
17     当 coroutine 执行完毕后 raises StopIteration, 这个异常的 value 属性是执行返回的结果.
18     如果 coroutine raises an exception, 这个异常会被'冒泡式' 返回. Coroutines 不可以直接
19     raise 一个未加工过的 StopIteration exceptions.
20 
21     Coroutines 有如下方法(与 generator 相似), 但是与 generators 不同的是 coroutines 不对
22     迭代提供直接的支持. Python version 3.5.2, 之后 在一个 coroutine 上多次 await 返回
23     RuntimeError exception.
24 
25         coroutine.send(value)
26             开始或者恢复 coroutine 的执行. 如果参数 value = None, 为对 coroutine 的预激活.
27             若 value 不是 None, 这个方法相当于 iterator 的 send() 方法, 将使 coroutine 暂停.
28             方法的返回 (return value, StopIteration, or other exception) 上面已经描述过.
29 
30         coroutine.throw(type[, value[, traceback]])
31             在 coroutine raise specified exception.
32             这个方法对应 iterator 中的 throw() 方法, 会使 coroutine 暂停
33 
34         coroutine.close()
35             使 coroutine 去初始化并退出. 对应 iterator 的 close() 方法.
36 
37 Asynchronous Iterators
38     一个 asynchronous iterable 通过调用 __aiter__() 得到一个 asynchronous iterator.
39     Asynchronous iterators 可以被用在一个 async 声明中.
40 
41     object.__aiter__(self)
42         返回一个 asynchronous iterator 对象.
43 
44     object.__anext__(self)
45         从 iterator 中返回一个 awaitable 对象. 当 iterator 结束的时候
46         raise  StopAsyncIteration error exception
47 
48     asynchronous iterable object 的例子,
49 
50         class Reader:
51             async def readline(self):
52                 ...
53 
54             def __aiter__(self):
55             return self
56 
57             async def __anext__(self):
58                 val = await self.readline()
59                 if val == b'':
60                     raise StopAsyncIteration
61                 return val
62 
63 Asynchronous Context Managers,
64     asynchronous context manager 是一个能够暂停执行的 context manager .
65     Asynchronous context managers 可以通过 async with 关键字声明.
66     object.__aenter__(self)
67         与 __enter__() 类似, 不同之处在于 方法必须返回一个 awaitable 对象.
68 
69     object.__aexit__(self, exc_type, exc_value, traceback),
70         与 __exit__() 类似, 不同之处在于 方法必须返回一个 awaitable 对象.
71 
72     asynchronous context manager 的例子,
73         class AsyncContextManager:
74             async def __aenter__(self):
75                 await log('entering context')
76 
77             async def __aexit__(self, exc_type, exc, tb):
78                 await log('exiting context')
79 
80 Reference,
81     Python doc. https://docs.python.org/3/reference/datamodel.html#coroutines