Created
August 5, 2021 22:04
-
-
Save adriangb/22a9ee5337716264b22d9a4ef96bc16b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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