Skip to content

AsyncClient ignores the startup and shutdown events #9125

Discussion options

You must be logged in to vote

It's all discussed in encode/httpx#350 and related issues, but I'm gonna leave here a short summary.

As described in encode/httpx#1441, app lifecycle managemenent won't be added to HTTPX's AsyncClient because its considered out of its scope.

Suggested solution for original problem is to use LifespanManager from asgi-lifespan in pair with AsyncClient.

So test_startup.py could be changed to:

import pytest

from asgi_lifespan import LifespanManager
from httpx import AsyncClient

from main import app


@pytest.fixture()
async def client():
    async with AsyncClient(app=app, base_url="http://test") as client, LifespanManager(app):
        yield client


@pytest.mark.asyncio
async def test_rea…

Replies: 20 comments

Comment options

Kludex
Sep 2, 2020
Collaborator Sponsor

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

Kludex
Sep 2, 2020
Collaborator Sponsor

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by Kludex
Comment options

Kludex
Mar 30, 2021
Collaborator Sponsor

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
10 participants
Converted from issue

This discussion was converted from issue #2003 on February 28, 2023 18:42.