Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can FastAPI guarantee a sync handler will never block the main application thread? [duplicate]

I have the following FastAPI application:

from fastapi import FastAPI
import socket

app = FastAPI()


@app.get("/")
async def root():
    return {"message": "Hello World"}


@app.get("/healthcheck")
def health_check():
    result = some_network_operation()
    return result


def some_network_operation():
    HOST = "192.168.30.12" # This host does not exist so the connection will time out
    PORT = 4567

    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        s.settimeout(10)
        s.connect((HOST, PORT))
        s.sendall(b"Are you ok?")

        data = s.recv(1024)
        print(data)

This is a simple application with two routes:

  • / handler that is async
  • /healthcheck handler that is sync

With this particular example, if you call /healthcheck, it won't complete until after 10 seconds because the socket connection will timeout. However, if you make a call to / in the meantime, it will return the response right away because FastAPI's main thread is not blocked. This makes sense because according to the docs, FastAPI runs sync handlers on an external threadpool.

My question is, if it is at all possible for us to block the application (block FastAPI's main thread) by doing something inside the health_check method.

  • Perhaps by acquiring the global interpreter lock?
  • Some other kind of lock?
like image 862
Dacite Avatar asked Apr 21 '26 07:04

Dacite


1 Answers

Yes, if you try to do sync work in a async method it will block FastAPI, something like this:

@router.get("/healthcheck")
async def health_check():
    result = some_network_operation()
    return result

Where some_network_operation() is blocking the event loop because it is a synchronous method.

like image 129
JarroVGIT Avatar answered Apr 23 '26 02:04

JarroVGIT