简体   繁体   中英

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

I am trying to reuse the code shown in the following SO post answer:

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?

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.

Below is an equivalent for non-async code:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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