简体   繁体   English

python asyncio上下文管理器:将2个“上下文”合并到1个变量中

[英]python asyncio context manager: combine 2 “contexts” in 1 variable

I am trying to reuse the code shown in the following SO post answer: 我正在尝试重用以下SO post答案中显示的代码:

Especially the part that does: 特别是执行以下操作的部分:

async with sema, session.get(url) as response:
    response = await response.read()

But I don't understand it.. how can 2 "contexts" (sema and session.get(url)) be combined into 1 variable? 但是我不明白..如何将2个“上下文”(sema和session.get(url))组合为1个变量?

Can anyone give me a quick explanation of that syntax? 谁能给我快速解释一下该语法吗?

Value from sema 's __aenter__ method is simply threw away and not assigned to any local variable. sema__aenter__方法的值被简单地丢弃,而不分配给任何局部变量。

Below is an equivalent for non-async code: 下面是等效的非异步代码:

with ctx1, open(file) as fh:
    content = fh.read()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM