英文:
Benchmarks of FastAPI vs async Flask?
问题
I'm a developer without an interest in benchmarking and I'm trying to decide whether I should use Flask or FastAPI to build some Python/Vue projects. I'm seeing stuff online about how FastAPI was faster than Flask because Flask was single-threaded or something like that, whereas FastAPI was async, but apparently more-recently Flask added async routes, and so now I'm wondering if FastAPI is still(?) faster than Flask.
Has anyone done benchmarking tests comparing FastAPI to Flask async routes? I can't find any when I search Google.
英文:
I'm a developer without an interest in benchmarking and I'm trying to decide whether I should use Flask or FastAPI to build some Python/Vue projects. I'm seeing stuff online about how FastAPI was faster than Flask because Flask was single-threaded or something like that, whereas FastAPI was async, but apparently more-recently Flask added async routes, and so now I'm wondering if FastAPI is still(?) faster than Flask.
Has anyone done benchmarking tests comparing FastAPI to Flask async routes? I can't find any when I search Google.
答案1
得分: 8
根据Miguel Grinberg的基准研究,FastAPI的性能可能比异步的Flask快或慢,这取决于Web服务器和Flask异步类型。通常情况下,由Greenlet提供支持的WSGI服务器(如Meinheld / Gevent)上的Flask可以提供与异步优先的ASGI框架FastAPI相当的吞吐量。请注意,Grinberg正在比较三个部分的整体性能:框架、Web服务器和Web应用程序。
以下是他尝试的三种不同情景中相关框架的吞吐量(数字越高越好):
框架 | Web服务器 | 类型 | 测试1吞吐量 | 测试2吞吐量 | 测试3吞吐量 |
---|---|---|---|---|---|
Flask | Meinheld | 异步 / Greenlet | 1.43 | 5.27 | 1.06 |
Flask | Gevent | 异步 / Greenlet | 1.22 | 4.54 | 1.01 |
FastAPI | Uvicorn | 异步 / 协程 | 1.21 | 4.33 | 1.02 |
Aioflask | Uvicorn | 异步 / 协程 | 1.11 | - | - |
Flask | uWSGI | 同步 | 1.09 | 1.01 | 1.26 |
Flask | Gunicorn | 同步 | 1.00(基准) | 1.00(基准) | 1.00(基准) |
- Meinheld是一个用C编写的WSGI服务器。
Grinberg在他的文章中指出,框架之间的确切相对结果将取决于服务器所承受的特定负载,但他的结论是,框架之间没有太大的差异:
请记住,不同框架或Web服务器之间的性能差异不会很显著,所以选择可以提高生产力的工具吧!
Flask的更多异步方法
Flask采用了不同的异步视图方法。在Grinberg的基准测试中,实施了三种方法:Aioflask(带有uvicorn的标准Python ASGI)、Meinheld中的Greenlet和Gevent。由于Aioflask测试没有完全完成,因此只有Test 1的结果可用,与Flask文档一致:
Flask的异步/协程支持性能不如异步优先框架,这是由于其实现方式所致。
请注意,Greenlet方法需要以特殊方式编写异步操作,而不是使用标准Python协程。因此,现有的asyncio代码库需要使用类似于greenletio的适配器进行修补。Gevent在很大程度上依赖于正确地对Python标准库、数据库引擎和其他性能关键库进行猴子补丁。
Grinberg在他的博客中多次提到了这一点:
Gevent在我的基准测试中表现还可以,但在原始测试中表现非常糟糕。这是因为作者忘记了对psycopg2包进行补丁,使其在greenlets下变为非阻塞状态。
英文:
According to a benchmark study by Miguel Grinberg,
FastAPI can be faster or slower than async Flask, depending on the web server and the Flask async type.
Generally Flask on a Greenlet powered WSGI server (Meinheld / Gevent) can offer comparable throughput as an async-first ASGI framework like FastAPI.
Note that Grinberg is comparing the overall performance of three parts: the Framework, the Web server and the Web application.
Here are the relevant frameworks' throughput in the three different scenarios he tried (a higher number is better):
Framework | Web Server | Type | Test 1 Throughput | Test 2 Throughput | Test 3 Throughput |
---|---|---|---|---|---|
Flask | Meinheld | Async / Greenlet | 1.43 | 5.27 | 1.06 |
Flask | Gevent | Async / Greenlet | 1.22 | 4.54 | 1.01 |
FastAPI | Uvicorn | Async / Coroutine | 1.21 | 4.33 | 1.02 |
Aioflas | Uvicorn | Async / Coroutine | 1.11 | - | - |
Flask | uWSGI | Sync | 1.09 | 1.01 | 1.26 |
Flask | Gunicorn | Sync | 1.00 (baseline) | 1.00 (baseline) | 1.00 (baseline) |
* Meinheld is a WSGI server written in C.
Grinberg points out in his article that the exact relative results between frameworks will depend on the particular load that the server is put under, but his takeaway is that there isn't a large difference between frameworks:
> remember that the difference in performance between different frameworks or web servers isn't going to be very significant, so choose the tools that make you more productive!
More about Async approaches of Flask
Flask adopts different approaches to async view.
In Grinberg's benchmark tests, three approaches have been implemented: Aioflask (standard python ASGI with uvicorn), Greenlet in Meinheld and Gevent. Since the Aioflask test was not fully finished, only the result of Test 1 is available, which agrees with the Flask documentation:
> Flask’s async/coroutine support is less performant than async-first frameworks due to the way it is implemented.
Be aware that the Greenlet approach requires a special way of writing asynchronous operations, instead of standard python coroutines. Therefore, existing asyncio codebase needs to be patched with adapters like greenletio.
Gevent relies heavily on proper monkey patching the python standard libraries, database engines and other performance critical libraries.
Grinberg addressed multiple times this in his blog:
> Gevent tests did reasonably well in my benchmark and terribly in the original one. This is because the author forgot to patch the psycopg2 package so that it becomes non-blocking under greenlets.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论