Question: for below code, will the context gets closed correctly when async_task() finishes?

1
2
with get_context():
await async_task()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/usr/bin/env python3

import asyncio as aio


class Ctx:
def __enter__(self):
print("enter")

def __exit__(self, exc_type, exc_val, exc_tb):
print("exit")


async def main():
with Ctx() as ctx:
print("sleeping")
await aio.sleep(2)


loop = aio.get_event_loop()
loop.run_until_complete(main())

This test produces:

enter
sleeping
exit

So yes - context manager does work with await operation inside it.