Z. *_*Qui 4 python asynchronous contextmanager
在 Python Lan 参考文献中。3.4.4,据说__aenter__()
并且__aexit__()
必须返回awaitables。但是,在示例异步上下文管理器中,这两个方法返回 None:
class AsyncContextManager:
async def __aenter__(self):
await log('entering context')
async def __aexit__(self, exc_type, exc, tb):
await log('exiting context')
Run Code Online (Sandbox Code Playgroud)
这段代码正确吗?
__aenter__
并且__aexit__
必须返回等待对象,但是看看当您调用示例中的等待对象时会发生什么:
>>> class AsyncContextManager:
... async def __aenter__(self):
... await log('entering context')
... async def __aexit__(self, exc_type, exc, tb):
... await log('exiting context')
...
>>> AsyncContextManager().__aenter__()
<coroutine object AsyncContextManager.__aenter__ at 0x7f5b092d5ac0>
Run Code Online (Sandbox Code Playgroud)
它没有回来None
!我们得到了一个协程对象,它是可等待的。
这些方法是async
函数,它们自动返回(可等待的)异步协程。return
函数体中的语句async
决定了协程时返回的内容await
,而不是调用函数时返回的内容。
这类似于生成器函数如何返回生成器迭代器,即使它们通常没有return
语句,以及如果您编写__iter__
为生成器函数,则不应尝试return
在生成器函数内使用迭代器。
那么,如果您确实将 a 放入return
或__aenter__
定义__aexit__
为async
函数,会发生什么情况?好吧,您可以,如果您这样做,该return
语句不必返回可等待的。这就是 Python 将要做的事情。
如果您将return
某些内容__aenter__
定义为async
函数,那么它决定了绑定到as
目标的内容(如果async with
使用as
.
如果您将return
某些内容__aexit__
定义为async
函数,则确定是否抑制块内引发的异常。“真”值告诉async with
抑制异常,而“假”值告诉async with
让异常传播。默认None
值为 false,因此默认情况下不会抑制异常。
这是一个例子:
import asyncio
class Example:
async def __aenter__(self):
return 3
async def __aexit__(self, exc_type, exc, tb):
return True
async def example():
async with Example() as three:
print(three == 3)
raise Exception
print("Exception got suppressed")
asyncio.run(example())
Run Code Online (Sandbox Code Playgroud)
True
Exception got suppressed
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
7474 次 |
最近记录: |