Home > Backend Development > Python Tutorial > How Can httpx Solve `h11._util.LocalProtocolError` When Making Concurrent Downstream HTTP Requests in FastAPI?

How Can httpx Solve `h11._util.LocalProtocolError` When Making Concurrent Downstream HTTP Requests in FastAPI?

Barbara Streisand
Release: 2024-12-28 07:07:24
Original
186 people have browsed it

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
Copy after login

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?

  • Async API: httpx offers an async API, allowing for asynchronous handling of HTTP(s) requests.
  • Connection Pooling: The httpx.AsyncClient() instance reuses TCP connections for multiple requests to the same host, optimizing performance.
  • Stream Support: httpx provides built-in streaming response handling for both incoming and outgoing requests.

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())
Copy after login

Additional Tips:

  • Use a lifespan handler to initialize and close the httpx client.
  • Consider using streaming responses to avoid memory consumption on the server side.
  • Control the connection pool size with the limits keyword argument on the httpx Client.

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template