How to share a httpx.AsyncClient() across the app? #1549
-
I need to use httpx.AsyncClient() and I've read in their docs it's more optimal to have a single client that you share across the app vs creating a client each time in the route handler. How can I share this client so that is available across all routes and then I can safely close it on app shutdown? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
I am not aware of this. However, there's nothing preventing you from doing that. You can just create an instance of the client and then import that. If you want something that's specifically bound to the lifetime of the application, you could of course use application state, dependency injection and the lifetime hooks. from litestar import Litestar
from litestar.datastructures import State
from litestar.di import Provide
from httpx import AsyncClient
def on_startup(state: State) -> None:
state.client = AsyncClient()
async def on_shutdown(state: State) -> None:
await state.client.aclose()
def get_client(state: State) -> AsyncClient:
return state.client
app = Litestar(
on_startup=[on_startup],
on_shutdown=[on_shutdown],
dependencies={"client": Provide(get_client)}
) |
Beta Was this translation helpful? Give feedback.
-
This small code gives error for me:
I ran it with uvicorn as in most examples PS It is my first discussion, so sorry for insufficient info |
Beta Was this translation helpful? Give feedback.
I am not aware of this. However, there's nothing preventing you from doing that. You can just create an instance of the client and then import that.
If you want something that's specifically bound to the lifetime of the application, you could of course use application state, dependency injection and the lifetime hooks.