Home > Backend Development > Python Tutorial > How to Efficiently Handle Downstream HTTP Requests in FastAPI/Uvicorn Using httpx?

How to Efficiently Handle Downstream HTTP Requests in FastAPI/Uvicorn Using httpx?

Linda Hamilton
Release: 2024-12-12 18:57:11
Original
717 people have browsed it

How to Efficiently Handle Downstream HTTP Requests in FastAPI/Uvicorn Using httpx?

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

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

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

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

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!

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