0

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?

jim jarnac
  • 4,804
  • 11
  • 51
  • 88

1 Answers1

1

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()
Łukasz Rogalski
  • 22,092
  • 8
  • 59
  • 93