Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support startup/shutdown events using an async contextmanager #67

Open
graingert opened this issue Nov 25, 2024 · 1 comment
Open

support startup/shutdown events using an async contextmanager #67

graingert opened this issue Nov 25, 2024 · 1 comment
Assignees
Labels
enhancement New feature or request

Comments

@graingert
Copy link

eg, I'd like to be able to run background tasks and pool http connections:

@contextlib.asynccontextmanager
async def lifespan_context():
    async with anyio.create_task_group() as tg, httpx.AsyncClient() as client:
        yield {"tg": tg, "client": client}

app = Server("name", lifespan_context=lifespan_context)

anything yielded from the context manager should be available on app.request_context.state

@dsp-ant
Copy link
Member

dsp-ant commented Nov 25, 2024

Thank you for the suggestion. I think that makes a lot of sense. Since FastAPI (and I assume others) do the same, it probably is the right pattern. I'll self-assign and see that I get to implement this. Of course we also accept PRs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants