[Python] Fastapi python code execution speed impacted by deployment with uvicorn vs gunicorn

Discussão em 'Python' iniciado por Stack, Agosto 22, 2025.

  1. Stack

    Stack Membro Participativo

    I wrote an fastapi app. And now I am thinking about deploying it however I seem to get strange unexpected performance issues that seem to depend on wether I use uvicorn vs gunicorn. In particular all code (even standard library pure python code) seems to get slower if I use gunicorn. For performance debugging I wrote a small app that demonstrates this:

    import asyncio, time
    from fastapi import FastAPI, Path
    from datetime import datetime

    app = FastAPI()

    @app.get("/delay/{delay1}/{delay2}")
    async def get_delay(
    delay1: float = Path(..., title="Nonblocking time taken to respond"),
    delay2: float = Path(..., title="Blocking time taken to respond"),
    ):
    total_start_time = datetime.now()
    times = []
    for i in range(100):
    start_time = datetime.now()
    await asyncio.sleep(delay1)
    time.sleep(delay2)
    times.append(str(datetime.now()-start_time))
    return {"delays":[delay1,delay2],"total_time_taken":str(datetime.now()-total_start_time),"times":times}


    Running the fastapi appi with:

    gunicorn api.performance_test:app -b localhost:8001 -k uvicorn.workers.UvicornWorker --workers 1


    The resonse body of a get to http://localhost:8001/delay/0.0/0.0 is consistently something like:

    {
    "delays": [
    0.0,
    0.0
    ],
    "total_time_taken": "0:00:00.057946",
    "times": [
    "0:00:00.000323",
    ...smilar values omitted for brevity...
    "0:00:00.000274"
    ]
    }


    However using:

    uvicorn api.performance_test:app --port 8001


    I consitently get timings like these

    {
    "delays": [
    0.0,
    0.0
    ],
    "total_time_taken": "0:00:00.002630",
    "times": [
    "0:00:00.000037",
    ...snip...
    "0:00:00.000020"
    ]
    }


    The difference becomes even more prounced when I uncomment the await asyncio.sleep(delay1) statement.

    So I am wondering what gunicorn/uvicorn do to the python/fastapi runtime to create this factor 10 difference in the speed of code execution.

    For what is is worth I performed these tests using Python 3.8.2 on OS X 11.2.3 with an intel I7 processor.

    And these are the relevant parts of my pip freeze output

    fastapi==0.65.1
    gunicorn==20.1.0
    uvicorn==0.13.4

    Continue reading...

Compartilhe esta Página