On this page
  1. contextlib.asynccontextmanager
  2. Import
  3. Signature & Overview
  4. Usage
  5. Gotchas
  6. REPL Inspect

contextlib.asynccontextmanager

Prev: contextlib.aclosing | Up: contextlib | Next: contextlib.AsyncExitStack
Rel: contextlib.AbstractAsyncContextManager, contextlib.AbstractContextManager, contextlib.aclosing, contextlib.AsyncExitStack
Ref: docs.python.org — contextlib.asynccontextmanager
Tags: #public #python3 #stdlib #contextlib


Import

import contextlib
from contextlib import asynccontextmanager

Signature & Overview

contextlib.asynccontextmanager(func)

@asynccontextmanager decorator.

Usage

import asyncio
from contextlib import asynccontextmanager

@asynccontextmanager
async def opened(name: str):
    yield f"conn:{name}"

async def main():
    async with opened("db") as c:
        assert c == "conn:db"

asyncio.run(main())

Gotchas

REPL Inspect

from contextlib import asynccontextmanager
help(asynccontextmanager)

Linked from

Related notes