Sobes.tech
Middle

How will a synchronous handle call affect the behavior of the entire FastAPI server?

sobes.tech AI

Answer from AI

FastAPI is built on an asynchronous framework that allows handling multiple requests simultaneously thanks to async/await.

If a synchronous (blocking) function is called in a FastAPI request handler, this call will block the current execution thread, leading to reduced performance and fewer requests being processed concurrently.

For example, if the server uses a single worker process, other requests will wait until the synchronous function completes.

To avoid this, it is recommended to:

  • Use asynchronous functions and libraries
  • If it is necessary to call synchronous code, execute it in a separate thread using run_in_executor

Example of calling a synchronous function in FastAPI using run_in_executor:

import asyncio
from fastapi import FastAPI
import time

app = FastAPI()

def blocking_io():
    time.sleep(5)  # blocking operation
    return "done"

@app.get("/sync")
async def read_sync():
    loop = asyncio.get_running_loop()
    result = await loop.run_in_executor(None, blocking_io)
    return {"result": result}

Thus, calling a synchronous function directly in a FastAPI handler can negatively impact server performance.