Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Discussion options

You must be logged in to vote

If you don't like approach with lifespan and request.state, you can wrap your cached dependency function with async function:

@lru_cache()
def _get_client() -> Client:
  return Client( ... )

async def get_client_async() -> Client:
    return _get_client()

@app.get('/')
async def my_endpoint(client: Annotated[Client, Depends(get_client_async)]) -> dict[str, any] :
  data = await client.fetch()
  return data

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@YuriiMotov
Comment options

Answer selected by YuriiMotov
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Question or problem
3 participants