惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
小众软件
小众软件
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - Franky
Jina AI
Jina AI
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Y
Y Combinator Blog
V
Visual Studio Blog
C
Check Point Blog
阮一峰的网络日志
阮一峰的网络日志
U
Unit 42
量子位
人人都是产品经理
人人都是产品经理
博客园 - 聂微东
M
MIT News - Artificial intelligence
爱范儿
爱范儿
B
Blog RSS Feed
MyScale Blog
MyScale Blog
H
Help Net Security
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
美团技术团队
L
LangChain Blog
D
Docker

ashishb.net

A day in Luxembourg - the richest country in the world I was asked to install malware during a fake interview Book summary: Breakneck - China's quest to engineer the future by Dan Wang Book summary: How to Teach Your Baby to Read Book Summary: The Discontented Little Baby Book by Pamela Douglas Introducing Amazing Sandbox - run third-party tools and AI agents securely on your machine Why software outsourcing gets a bad reputation? Book summary: The Natural Baby Sleep Solution by Polly Moore A day in Antwerp, Belgium Journey of online influencers Two days in Brussels, Belgium Shortcuts - when we love them and when we don't A visit to Rakhigarhi Three days in overhyped Paris Empty Japan, crowded Tokyo The real lock-in in GitHub is not the code, but the stars 11-day Norwegian Breakaway East Caribbean cruise Sanskrit and Sri Lankan Air Force Use REST with Open API The Achilles heel of American capitalism Costa Rica in 4 days At a juice stall in Sri Lanka A short stay at Warsaw, Poland Best practices for using Python & uv inside Docker Two days in Vilnius, Lithuania How IntelliJ IDEs waste disk space Pregnancy Why there aren't many digital nomads from India Two days in Riga, Latvia To keep your machine secure, run third-party tools inside Docker
FastAPI vs Flask performance comparison
Ashish Bhatia · 2025-05-10 · via ashishb.net

If you are running Python in production, you will almost certainly have to decide which web framework to use.

Let’s consider a rudimentary Hello world based test comparing the performance of two popular web frameworks for Python - FastAPI and Flask.

I will intentionally use Docker for benchmarking as most deployments today will explicitly or implicitly rely on Docker.

For Flask, I will use this

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
# Build: docker buildx build -t python-flask -f Dockerfile_python .
# Size: docker image inspect python-flask --format='{{.Size}}' | numfmt --to=iec-i
# Run: docker run -it --rm --cpus=1 --memory=100m -p 8001:8001 python-flask
FROM python:3.13-slim AS base

WORKDIR /app
RUN pip3 install --no-cache-dir flask gunicorn
SHELL ["/bin/bash", "-c"]
RUN echo -e "\
from flask import Flask\n\
app = Flask(__name__)\n\
\
@app.get('/')\n\
def root():\n\
  return 'Hello, World!'\n\
" > /app/web_server.py

ENTRYPOINT ["gunicorn", "web_server:app", "--bind=0.0.0.0:8001", "--workers=4", "--threads=32"]

And for FastAPI, I will use this

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
# Build: docker buildx build -t python-fastapi -f Dockerfile_python .
# Size: docker image inspect python-fastapi --format='{{.Size}}' | numfmt --to=iec-i
# Run: docker run -it --rm --cpus=1 --memory=100m -p 8002:8002 python-fastapi
FROM python:3.13-slim AS base

WORKDIR /app
RUN pip3 install --no-cache-dir fastapi gunicorn uvicorn
SHELL ["/bin/bash", "-c"]
RUN echo -e "\
from fastapi import FastAPI\n\
app = FastAPI()\n\
@app.get('/')\n\
async def root():\n\
    return {'message': 'Hello World'}\
" > /app/web_server.py

ENTRYPOINT ["gunicorn", "web_server:app", "--bind=0.0.0.0:8002", \
  "-k uvicorn.workers.UvicornWorker", "--workers=4", "--threads=32"]

As you will notice the only difference is the framework used. The rest of the code is exactly the same.

Comparing the result, we can see FastAPI is 3X faster in these basic requests.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
$ h2load --h1 -n1000 -c40 'http://localhost:8001'  # Flask
...
                      min         max         mean         sd        +/- sd
time for request:      406us     83.37ms     12.56ms     22.46ms    86.70%
...
req/s           :      75.98       83.04       79.36        2.08    60.00%

$ h2load --h1 -n1000 -c40 'http://localhost:8002'  # FastAPI results
...
                      min         max         mean         sd        +/- sd
 time for request:      825us     29.78ms      4.07ms      6.13ms    91.70%
# req/s           :     231.07      256.41      241.98        8.86    52.50%
...

So, in terms of throughput, I am fairly convinced that no one should choose Flask over FastAPI for new projects.

Even if query serving time is only 20% of the end to end latency, one gets about 15% performance improvement by choosing FastAPI over Flask.

Even if you decide to use Flask, given its single threaded nature, use it behind gunicorn, the way I used above

It is definitely concerning though that FastAPI, as of 2025, is not stable release yet.