Skip to content

Instantly share code, notes, and snippets.

@adriangb
Created August 5, 2021 22:04
Show Gist options
  • Save adriangb/22a9ee5337716264b22d9a4ef96bc16b to your computer and use it in GitHub Desktop.
Save adriangb/22a9ee5337716264b22d9a4ef96bc16b to your computer and use it in GitHub Desktop.
import asyncio
from contextlib import asynccontextmanager
import contextvars
ctxvar = contextvars.ContextVar("ctx")
@asynccontextmanager
async def lifespan():
ctxvar.set("spam")
yield
async def endpoint():
assert ctxvar.get() == "spam"
async def main():
async with lifespan():
await endpoint() # repepatedly called
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment