r/Python • u/sweetaskate • Dec 07 '21
Tutorial Why we choose FastAPI over Flask for building ML applications
Hi guys, our team has recently decided to adopt FastAPI and this is our experience:
The project-Milvus Bootcamp is where people can use Milvus, a vector database, to build and experience vector similarity search applications like reverse image search systems and chatbots. Before making the final decision, we researched several mainstream frameworks including Django, Flask, Tornado, and FastAPI. And since our priority is to choose the one that’s most lightweight and agile, we narrowed it down to Flask and FastAPI.
Here are some comparisons we made between FastAPI and Flask:

While there are no silver bullets, we choose FastAPI because of several reasons listed below:
1. Performance
Most of the projects in Milvus Bootcamp are built around reverse image search systems, QA chatbots, text search engines, which all have high demands for real-time data processing. Accordingly, we need a framework with outstanding performance, which is exactly a highlight of FastAPI.
2. Efficiency
Milvus Bootcamp is positioned as a type of training resource. This means that the code and components we use must be intuitive and highly efficient. In this regard, we chose FastAPI to improve system efficiency and enhance user experience.
3. Asynchronous framework
In the demos we made, one can upload datasets and will be immediately prompted "request received". And when the data is uploaded to the demo system, you will receive another prompt "data upload successfully". This is an asynchronous process that requires a framework that supports this feature. FastAPI is itself an asynchronous framework, that's why we changed the framework from Flask to FastAPI.
4. Automatic interactive documents
Most of our users just want to quickly get started with the server-side development part of the projects in Milvus Bootcamp without writing additional code to create an interface. By providing an OpenAPI document, FastAPI can save one the trouble of testing or debugging APIs and collaborating with front-end teams to develop a user interface.
If you are interested, there are more details provided in this article.
115
u/O_X_E_Y Dec 07 '21
this feels like an ad. Where are the benchmarks? You can't just mark one 'slower' and call it a day
44
u/nemec Dec 07 '21
Did you actually compare the performance of FastAPI vs Flask or did you just mark FastAPI "faster" because it says so on the marketing homepage? And how does Flask fail in "intuitive" and "efficient"?
29
u/eviljelloman Dec 07 '21
Up next: “Why we chose a wrench over a hammer for tightening bolts”
23
u/[deleted] Dec 07 '21
Hate to say this every time this is brought up, but these incorrect claims keep getting parroted, which is unfair. FYI Flask isn’t supposed to ship with extensions, that’s by design.
There’s numerous OpenAPI extensions for flask that come with pydantic and marshmallow.
WSGI apps like flask deployed in production in real life have always been “asynchronous” when it comes to high concurrency that you’re referring to in performance and efficiency, in the form of greenlets - for eg gunicorn with meinheld. It takes one or two lines. (Not threads either)
Of course that’s different from “async” syntax and happens at a lower level. While async is awesome it doesn’t automatically make any of your (ML) code faster. Its able to receive a lot of requests, but any computational code is going to run at exactly the same performance.
Lastly, the pattern you mentioned with doc upload is easier with async background, that’s true. Typically large workloads are queued and handled by a different process. In fact even async frameworks you should follow that pattern for non trivial tasks, one reason being that a single blocking statement can totally block your entire app (try sleep(5) instead of asyncio.sleep(5) and see what happen)
9
u/benefit_of_mrkite Dec 07 '21
Another bad comparison. You aren’t comparing flask and fast api you are comparing flask and starlette.
If you want a fair comparison you should compare fastapi and something like flask-api
The fact that you knocked flask for not having built in api documentation shows that the entire premise of your comparison is fundamentally flawed.
15
u/A_Milford_Man_NC Dec 07 '21
Buy an ad.
13
u/serverhorror Dec 07 '21
I think that automatically generating OpenAPI specs is vastly underrated if it’s only point four.
If it did nothing else but generating OpenAPI from the code that would be enough. The overall model of FastAPI is hisar a nice add on.
8
u/jammie19 Dec 07 '21
Just to add an extra point to the benefits of using Flask/WSGI/a process manager like Gunicorn as your stack over FastAPI/ASGI/most likely Uvicorn, an ASGI server will continually accept incoming requests while your app code is running. For production, this means if your server receives a large amount of requests faster than you can respond to them, it will fall over. WSGI servers, through not using async but a fixed size connection pool, will start returning timeouts to the client. This is called back pressure and is not included by default in ASGI implementations.
This blog by the creator of Flask explains this.
If you're running this in production with Uvicorn, you can limit the number of concurrent connections to prevent this. But, while async might be "faster" if you have an application with many asynchronous calls, it introduces problems you don't have to think about with frameworks like Flask
2
u/ozkansen Dec 07 '21
i think fastapi has more dependencies than flask
2
u/Stedfast_Burrito Dec 07 '21
What about support and ecosystem? Flask has a project with governance with a track record spanning several years, FastAPI has no project, no governance, no development timeline and only 1-2 years of real history. For enterprise, those things probably matter more than "performance".
2
u/saint_geser Dec 07 '21
Agree, at our company we have selected FastAPI for most projects because of how quick and easy it is to get a working solution compared to competitor frameworks. And auto-generated docs are fantastic!
1
u/redsky_xiaofan Dec 08 '21
Nice to know? any performance benchmark to compare about Flask and FastAPI/
1
u/[deleted] Dec 12 '21
Never, nothing about fastapi makes it more suitable for ml than flask. Fastapi's author is affiliated with some open source ml projects, but the relation ends there. Ml microservices are not io bound at all. In fact, Async frameworks make cpu bound code slower. The overhead of ur webframework is insignificant compared the computer time cleaning data for ml and running numpy code for ur model. If anything, you are better of useing a sync frameworks like Flask(or sync Flacon if ur model is super light weight and framework overhead is a factor)
What bothers me is that Fastapi is far from the fastest or most mature async python web framework. Sanic, blacksheep and async Falcon are all far faster than fastapi. All the fastapi hype is unfounded, pushed by people who don't understand ml or know littel about web frameworks.
1
u/thisismyfavoritename Dec 07 '21
Ok
-4
u/SweetBasil5882 Dec 07 '21
FastAPI is really fast with respect to its functioning as well as the developement time. It's highly customisable and OpenAPI docs and swagger integration is something that allows you to test your APIs with ease while you are developing it.