Making Downstream HTTP Requests in FastAPI/Uvicorn Using httpx
Introduction
While using an API endpoint in FastAPI/Uvicorn that relies on external HTTP requests, it's crucial to ensure thread-safe handling of concurrency. This article explores the recommended approach to address this issue using the httpx library.
Using httpx
Inместоrequests, httpx offers an asynchronous API that supports multiple concurrent HTTP requests using a shared client. This improves performance by reusing connections and headers.
Implementing httpx in FastAPI
To use httpx in FastAPI, you can utilize its AsyncClient:
from fastapi import FastAPI from httpx import AsyncClient app = FastAPI() app.state.client = AsyncClient() @app.on_event("shutdown") async def shutdown_event(): await app.state.client.aclose()
In this example, a shared client is created as part of FastAPI's state, allowing it to be accessed by endpoints.
Async Example
The following endpoint makes an asynchronous HTTP request and streams the response back to the client:
from fastapi import FastAPI, StreamingResponse, BackgroundTask @app.get("/") async def home(): client = app.state.client req = client.build_request("GET", "https://www.example.com/") r = await client.send(req, stream=True) return StreamingResponse(r.aiter_raw(), background=BackgroundTask(r.aclose))
Updated Example
With the deprecation of startup and shutdown events, you can now employ a lifespan handler:
from fastapi import FastAPI, Request, lifespan from starlette.background import BackgroundTask from httpx import AsyncClient, Request @lifespan.on_event("startup") async def startup_handler(app: FastAPI): app.state.client = AsyncClient() @lifespan.on_event("shutdown") async def shutdown_handler(): await app.state.client.aclose() @app.get("/") async def home(request: Request): client = request.state.client req = client.build_request("GET", "https://www.example.com") r = await client.send(req, stream=True) return StreamingResponse(r.aiter_raw(), background=BackgroundTask(r.aclose))
Reading Response Content
If you need to read the response content on the server side before sending it to the client, you can use a generator:
def gen(): async for chunk in r.aiter_raw(): yield chunk await r.aclose() return StreamingResponse(gen())
Conclusion
By leveraging httpx and its shared async client, you can handle downstream HTTP requests efficiently within FastAPI/Uvicorn, ensuring thread safety and performance optimization in a multi-threaded environment.
The above is the detailed content of How to Efficiently Handle Downstream HTTP Requests in FastAPI/Uvicorn Using httpx?. For more information, please follow other related articles on the PHP Chinese website!