Skip to content

Instantly share code, notes, and snippets.

@adriangb
Last active August 5, 2021 22:05
Show Gist options
  • Save adriangb/b5f453eec66c5c6694364cb8e5891af3 to your computer and use it in GitHub Desktop.
Save adriangb/b5f453eec66c5c6694364cb8e5891af3 to your computer and use it in GitHub Desktop.
import asyncio
import contextvars
ctxvar = contextvars.ContextVar("ctx")
async def lifespan():
ctxvar.set("spam")
return contextvars.copy_context()
async def endpoint():
assert ctxvar.get() == "spam"
async def main():
ctx = await asyncio.create_task(lifespan())
await asyncio.create_task(ctx.run(endpoint)) # doesn't work, just an example
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment