(Nov-15-2018, 04:57 PM)heras Wrote: Maybe I can also use asynccontextmanager to get the structure I want.
I guess you can do this. Using
await
in the context, requires to be inside a async
function.EDIT:
You can't mix synchronous and asynchronous context managers :-(
You can stack them only.
Calling a blocking synchronous function in async code, will block the whole event-loop.
What works:
In [35]: @contextlib.asynccontextmanager ...: async def async_ctx(): ...: print('Entering async context') ...: await asyncio.sleep(5) ...: yield ...: print('Leaving async context') ...: In [36]: @contextlib.contextmanager ...: def sync_ctx(): ...: print('Entering sync context') ...: yield 42 ...: print('Leaving sync context') In [37]: async def run(): ...: async with async_ctx(): ...: with sync_ctx() as sync_ret: ...: print(sync_ret) In [38]: asyncio.run(run()) Entering async context Entering sync context 42 Leaving sync context Leaving async context
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
All humans together. We don't need politicians!