Optimizing JSON Data Retrieval Speed in FastAPI
The sluggish return of a sizable JSON payload from FastAPI's GET endpoint is a recurring issue. When using json.dumps() to transmit data from a file using json.loads(), the response is vastly delayed. While return data can be used to send data to the user, is there a more efficient approach?
Problem:
The processing pipeline initially transforms the data into JSON using pandas' to_json() function, then loads it into a dictionary with json.loads(), and finally translates it back to JSON. This multi-step conversion process introduces substantial latency.
Proposed Solution:
First, it is crucial to recognize that FastAPI converts return values into JSON-compatible data using the jsonable_encoder, followed by serialization using the standard Python's json.dumps() function. This two-step process is known to be slow.
Option 1: Utilize Alternative JSON Encoders
Consider using alternative JSON encoders like orjson or ujson. These encoders outperform the default jsonable_encoder and json.dumps() combination.
Option 2: Direct Return of Custom Response
For optimal performance, use the custom APIRoute class and return a Response object. This bypasses FastAPI's default JSON conversion process.
<code class="python">from fastapi.routing import APIRouter, APIRoute class TimedRoute(APIRoute): ... app = FastAPI() router = APIRouter(route_class=TimedRoute) @router.get("/custom-response") def get_data(): df = pd.read_parquet('data.parquet') return Response(df.to_json(orient="records"), media_type="application/json") app.include_router(router)</code>
Additional Considerations:
The above is the detailed content of How to Optimize JSON Data Retrieval Speed in FastAPI?. For more information, please follow other related articles on the PHP Chinese website!