Home >Backend Development >Python Tutorial >How Can httpx Solve `h11._util.LocalProtocolError` When Making Concurrent Downstream HTTP Requests in FastAPI?
Making Downstream HTTP Requests with Uvicorn/FastAPI
Issue:
When sending multiple concurrent requests to an API endpoint hosted on Uvicorn/FastAPI, an error is encountered:
h11._util.LocalProtocolError: can't handle event type ConnectionClosed when role=SERVER and state=SEND_RESPONSE
Solution:
To resolve this issue and handle downstream HTTP requests efficiently within FastAPI, consider using httpx instead of the traditional requests library.
Why use httpx?
Example Usage:
The following code demonstrates the use of httpx within a FastAPI endpoint:
from fastapi import FastAPI, StreamingResponse from httpx import AsyncClient app = FastAPI() @app.on_event("startup") async def startup_event(): app.state.client = AsyncClient() @app.on_event("shutdown") async def shutdown_event(): await app.state.client.aclose() @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())
Additional Tips:
By adopting httpx and implementing the suggested best practices, you can effectively handle downstream HTTP requests within your Uvicorn/FastAPI application.
The above is the detailed content of How Can httpx Solve `h11._util.LocalProtocolError` When Making Concurrent Downstream HTTP Requests in FastAPI?. For more information, please follow other related articles on the PHP Chinese website!