pythonpython-asynciopython-3.7

It there any default asynchronious null context manager in python3.7+?


I would like to create optional asynchronious semaphore.
In case of asyncio.Semaphore does not support None values, i decided to create asyncio.Semaphore, if connections limit is specified, else - some kind of dummy object
There is a contextlib.nullcontext, but it supports only synchorious with
I`ve created my own dummy:

@contextlib.asynccontextmanager
async def asyncnullcontext():
    yield None

It there any default asynchronious null context manager?


Solution

  • It there any default asynchronious null context manager?

    You can use contextlib.AsyncExitStack().

    ExitStack() was similarly the way to create a quick-and-dirty null context manager before the introduction of nullcontext.