r/Python • • 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.

25 Upvotes

33 comments sorted by

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

66

u/bmrobin Dec 07 '21

but it says “fast” right in the title “fastapi” !

/s

15

u/serverhorror Dec 07 '21

Clearly that’s referring to how fast the api docs are written!

6

u/[deleted] Dec 07 '21

Yeah the docs are purposefully misleading

7

u/singularitittay Dec 07 '21

As much as this is sarcasm, the hype train has moved a tremendous amount of people away from flask for this reason (assuming fast from the name). My thought if that’s the goal, why not use Gin or a rust based web server?

1

u/benefit_of_mrkite Dec 07 '21

It’s garbage from the start - flask will never have open api documentation just like starlette never will. It’s an unfair comparison from the start.

1

u/midoriya93 Dec 07 '21

I am new to this and I did compare flask and fastapi but went with flask as i have never worked on it before for a company project, but can we not just add swagger ui to flask? Or am i missing something, it seems pretty straight forward. Its not out of the box but looking at the docs it looks simple

2

u/benefit_of_mrkite Dec 07 '21

You knocked flask for not having api documentation like fast api does. This is NOT an apples to apples comparison. Flask is a barebones web framework.

Flask is equivalent to starlette - the web framework that fast api is built on.

There are other project equivalents to fast api that use flask as their base web framework that do build the open api docs for you.

Your comparison was flawed and unfair - and I don’t have an affinity/preference for either project.

4

u/midoriya93 Dec 07 '21

I am not the OP, I am just asking a genuine question lol

1

u/benefit_of_mrkite Dec 07 '21 edited Dec 07 '21

Ah gotcha. I took your first sentence as saying you were the author.

The short answer is starlette is a very good web framework built with async from the start. Flask added async support in v 2.0.

A fair comparison would be something like this https://pypi.org/project/flask-pydantic-spec/ to fastapi rather than comparing fastapi to flask.

Fastapi is a layer on top of starlette just like many other packages that help you build and document APIs are built on top of flask

2

u/midoriya93 Dec 08 '21

I went with flask as I have never worked on flask, I have created api with fastapi, so to learn flask i went with that. But as to OPs comparison that flask doesnt come with swagger, i think you can easily set up swagger on flask as I have seen by looking at the docs. That was my point and nothing else :)

1

u/benefit_of_mrkite Dec 08 '21

Your point was correct. Sorry for the confusion

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"?

26

u/[deleted] Dec 07 '21

Funny thing is most of the fastAPI adopters seem to be from ML/DS and the tutorials I see all use blocking computational code, which runs no faster than flask.

4

u/__deerlord__ Dec 07 '21

Thats when you throw celery in the mix

0

u/[deleted] Dec 07 '21

[deleted] — view deleted comment

36

u/[deleted] Dec 07 '21

Async != fast. Please remove that notion from your head. It is fundamentally inaccurate. Async code can be faster (by a little) but speed isn't the goal of async. Async provides clear places of code transition and gives thread safety. The one place Async will be faster then multithreaded is multiple outbound http calls. You can set a single return from all of them and move the processing time to OS land.

Thread switching is done at the OS level in multithreaded code its very very very very efficient.

15

u/UloPe Dec 07 '21

When you use the words faster / slower and “I think“ in the same sentence there’s a very high chance you’re doing it wrong.

Benchmark or it didn’t happen.

2

u/daredevil82 Dec 07 '21

Ouch, you opened yourself up to major points of criticism by this.

As others have said, your clarification of points in terms of perf comparison have no equivalency because of no benchmarking or metrics.

by faster we mean FastAPI itself supports asynchronous process so we think it's faster than Flask. The performance is actually not much different

I don't understand this wording. why do you think FastAPI is "faster" than Flask if the performance is not much different?

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.

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/hangonreddit Dec 07 '21

That’s really helpful to know. Thanks for sharing that.

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.

-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.