当 __aenter__() 使用“async with”失败时会发生什么?

Dur*_*ndA 3 python python-3.x python-asyncio semantics aiohttp

PEP 492提到:

async with EXPR as VAR:
    BLOCK
Run Code Online (Sandbox Code Playgroud)

在语义上等同于:

mgr = (EXPR)
aexit = type(mgr).__aexit__
aenter = type(mgr).__aenter__

VAR = await aenter(mgr)
try:
    BLOCK
except:
    if not await aexit(mgr, *sys.exc_info()):
        raise
else:
    await aexit(mgr, None, None, None)
Run Code Online (Sandbox Code Playgroud)

然而,VAR = await aenter(mgr)它不在try区块中,所以我想知道是否__aenter__()允许失败。

例如,在此aiohttp片段中(摘自入门):

import aiohttp
import asyncio

async def main():

    async with aiohttp.ClientSession() as session:
        async with session.get('http://python.org') as response:

            print("Status:", response.status)
            print("Content-type:", response.headers['content-type'])

            html = await response.text()
            print("Body:", html[:15], "...")

loop = asyncio.get_event_loop()
loop.run_until_complete(main())
Run Code Online (Sandbox Code Playgroud)

session.get('http://python.org')可能会失败并且__aexit__()不会被调用来关闭上下文。

use*_*ica 6

如果__aenter__失败,__aexit__确实是无法运行。在这种情况下,任何必要的清理工作都是__aenter__的责任。

__aenter__有更多关于它走了多远以及什么已经或没有成功初始化的信息,因此处理这个比期望处理清理任意部分进入的上下文管理器状态__aenter__更方便。__aexit__

(这对于普通的非异步上下文管理器来说是完全相同的。)