Making HTTP Requests in Uvicorn/FastAPI
When handling HTTP endpoints built using FastAPI and Uvicorn, it's common to request data from external APIs. However, when dealing with multiple concurrent requests, errors such as "can't handle event type ConnectionClosed when role=SERVER and state=SEND_RESPONSE" may arise. This happens because the default HTTP client library, 'requests', is not fully thread-safe when utilized in such a concurrent environment.
To resolve this issue, consider implementing the alternative HTTP client library called httpx. It offers an async API that helps avoid thread safety issues within FastAPI. The example below showcases how to employ httpx in FastAPI:
from fastapi import FastAPI, Request, BackgroundTask from fastapi.responses import StreamingResponse, Response from contextlib import asynccontextmanager import httpx @asynccontextmanager async def lifespan(app: FastAPI): async with httpx.AsyncClient() as client: yield {'client': client} app = FastAPI(lifespan=lifespan) @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()))
By utilizing httpx's async API, you can handle HTTP requests within FastAPI more efficiently while maintaining thread safety. You can further customize the connection pool size using the 'limits' keyword argument on the Client object.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3