问题描述
我一直在研究Flask和FastAPI,以了解其如何充当服务器.
我想知道的主要事情之一是Flask和FastAPI如何处理来自多个客户端的多个请求.
尤其是当代码存在效率问题(数据库查询时间长)时.
I have been tinkering around Flask and FastAPI to see how it acts as a server.
One of the main things that I would like to know is how Flask and FastAPI deal with multiple request from multiple clients.
Especially when the code has efficiency issues (long database query time).
因此,我尝试编写一个简单的代码来了解此问题.
代码很简单,当客户端访问路由时,应用程序将休眠10秒钟,然后返回结果.
看起来像这样:
So, I tried making a simple code to understand this problem.
The code is simple, when a client access the route, the application sleeps for 10 seconds before it returns a results.
It looks something like this:
FastAPI
import uvicorn
from fastapi import FastAPI
from time import sleep
app = FastAPI()
@app.get('/')
async def root():
print('Sleeping for 10')
sleep(10)
print('Awake')
return {'message': 'hello'}
if __name__ == "__main__":
uvicorn.run(app, host="127.0.0.1", port=8000)
烧瓶
from flask import Flask
from flask_restful import Resource, Api
from time import sleep
app = Flask(__name__)
api = Api(app)
class Root(Resource):
def get(self):
print('Sleeping for 10')
sleep(10)
print('Awake')
return {'message': 'hello'}
api.add_resource(Root, '/')
if __name__ == "__main__":
app.run()
应用程序启动后,我尝试通过2个不同的chrome客户端同时访问它们.结果如下:
Once the applications are up, I tried accessing them on the same time through 2 different chrome clients.The below are the results:
FastAPI
烧瓶
如您所见,对于FastAPI,代码首先等待10秒,然后再处理下一个请求.而对于Flask,代码会在10秒睡眠仍在发生时处理下一个请求.
As you can see, for FastAPI, the code first waits 10 seconds before processing the next request. Whereas for Flask, the code processes the next request while the 10 second sleep is still happening.
尽管做了一些谷歌搜索,但是在这个话题上并没有一个直接的答案.
如果有人有任何建议可以说明这一点,请将其放在评论中.
Despite doing a bit of googling, there is not really a straight answer on this topic.
If anyone has any comments that can shed some light on this, please drop them in the comments.
您的意见都受到赞赏.非常感谢您抽出宝贵的时间.
You opinions are all appreciated. Thank you all very much for your time.
编辑关于此的更新,我正在探索更多,并发现了流程管理器的概念.例如,我们可以使用流程管理器(gunicorn)运行uvicorn.通过增加更多的工人,我能够实现Flask之类的东西.但是,仍在测试此限制. https://www.uvicorn.org/deployment/
EDITAn update on this, I am exploring a bit more and found this concept of Process manager. For example, we can run uvicorn using a process manager (gunicorn). By adding more workers, I am able to achieve something like Flask. Still testing the limits of this however.https://www.uvicorn.org/deployment/
感谢所有发表评论的人!欣赏它.
Thanks to everyone who left comments! Appreciate it.
推荐答案
这似乎有点有趣,所以我对ApacheBench
进行了一些测试:
This seemed a little interesting, so i ran a little tests with ApacheBench
:
烧瓶
from flask import Flask
from flask_restful import Resource, Api
app = Flask(__name__)
api = Api(app)
class Root(Resource):
def get(self):
return {"message": "hello"}
api.add_resource(Root, "/")
FastAPI
from fastapi import FastAPI
app = FastAPI(debug=False)
@app.get("/")
async def root():
return {"message": "hello"}
我为FastAPI运行了2个测试,但有很大的不同:
I ran 2 tests for FastAPI, there was a huge difference:
-
gunicorn -w 4 -k uvicorn.workers.UvicornWorker fast_api:app
-
uvicorn fast_api:app --reload
gunicorn -w 4 -k uvicorn.workers.UvicornWorker fast_api:app
uvicorn fast_api:app --reload
这是5000个并发为500的请求的基准测试结果:
So here is the benchmarking results for 5000 requests with a concurrency of 500:
带有Uvicorn Worker的FastAPI
Concurrency Level: 500
Time taken for tests: 0.577 seconds
Complete requests: 5000
Failed requests: 0
Total transferred: 720000 bytes
HTML transferred: 95000 bytes
Requests per second: 8665.48 [#/sec] (mean)
Time per request: 57.700 [ms] (mean)
Time per request: 0.115 [ms] (mean, across all concurrent requests)
Transfer rate: 1218.58 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 6 4.5 6 30
Processing: 6 49 21.7 45 126
Waiting: 1 42 19.0 39 124
Total: 12 56 21.8 53 127
Percentage of the requests served within a certain time (ms)
50% 53
66% 64
75% 69
80% 73
90% 81
95% 98
98% 112
99% 116
100% 127 (longest request)
FastAPI-纯Uvicorn
Concurrency Level: 500
Time taken for tests: 1.562 seconds
Complete requests: 5000
Failed requests: 0
Total transferred: 720000 bytes
HTML transferred: 95000 bytes
Requests per second: 3200.62 [#/sec] (mean)
Time per request: 156.220 [ms] (mean)
Time per request: 0.312 [ms] (mean, across all concurrent requests)
Transfer rate: 450.09 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 8 4.8 7 24
Processing: 26 144 13.1 143 195
Waiting: 2 132 13.1 130 181
Total: 26 152 12.6 150 203
Percentage of the requests served within a certain time (ms)
50% 150
66% 155
75% 158
80% 160
90% 166
95% 171
98% 195
99% 199
100% 203 (longest request)
用于烧瓶:
Concurrency Level: 500
Time taken for tests: 27.827 seconds
Complete requests: 5000
Failed requests: 0
Total transferred: 830000 bytes
HTML transferred: 105000 bytes
Requests per second: 179.68 [#/sec] (mean)
Time per request: 2782.653 [ms] (mean)
Time per request: 5.565 [ms] (mean, across all concurrent requests)
Transfer rate: 29.13 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 87 293.2 0 3047
Processing: 14 1140 4131.5 136 26794
Waiting: 1 1140 4131.5 135 26794
Total: 14 1227 4359.9 136 27819
Percentage of the requests served within a certain time (ms)
50% 136
66% 148
75% 179
80% 198
90% 295
95% 7839
98% 14518
99% 27765
100% 27819 (longest request)
总结果
烧瓶:测试时间:27.827秒
FastAPI-Uvicorn :测试时间:1.562秒
FastAPI-Uvicorn Workers :测试时间:0.577秒
使用Uvicorn Workers,FastAPI比Flask快了 48倍,这是可以理解的. ASGI vs WSGI ,所以我跑了1个同意:
With Uvicorn Workers FastAPI is nearly 48x faster than Flask, which is very understandable. ASGI vs WSGI, so i ran with 1 concurreny:
FastAPI-UvicornWorkers :测试时间:1.615秒
FastAPI-纯Uvicorn :测试时间:2.681秒
烧瓶:测试时间:5.541秒
与服务生同住的人
Server Software: waitress
Server Hostname: 127.0.0.1
Server Port: 8000
Document Path: /
Document Length: 21 bytes
Concurrency Level: 1000
Time taken for tests: 3.403 seconds
Complete requests: 5000
Failed requests: 0
Total transferred: 830000 bytes
HTML transferred: 105000 bytes
Requests per second: 1469.47 [#/sec] (mean)
Time per request: 680.516 [ms] (mean)
Time per request: 0.681 [ms] (mean, across all concurrent requests)
Transfer rate: 238.22 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 4 8.6 0 30
Processing: 31 607 156.3 659 754
Waiting: 1 607 156.3 658 753
Total: 31 611 148.4 660 754
Percentage of the requests served within a certain time (ms)
50% 660
66% 678
75% 685
80% 691
90% 702
95% 728
98% 743
99% 750
100% 754 (longest request)
带有独角兽工作者的独角兽
Server Software: uvicorn
Server Hostname: 127.0.0.1
Server Port: 8000
Document Path: /
Document Length: 19 bytes
Concurrency Level: 1000
Time taken for tests: 0.634 seconds
Complete requests: 5000
Failed requests: 0
Total transferred: 720000 bytes
HTML transferred: 95000 bytes
Requests per second: 7891.28 [#/sec] (mean)
Time per request: 126.722 [ms] (mean)
Time per request: 0.127 [ms] (mean, across all concurrent requests)
Transfer rate: 1109.71 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 28 13.8 30 62
Processing: 18 89 35.6 86 203
Waiting: 1 75 33.3 70 171
Total: 20 118 34.4 116 243
Percentage of the requests served within a certain time (ms)
50% 116
66% 126
75% 133
80% 137
90% 161
95% 189
98% 217
99% 230
100% 243 (longest request)
纯Uvicorn,但这次有4名工人uvicorn fastapi:app --workers 4
Pure Uvicorn, but this time 4 workers uvicorn fastapi:app --workers 4
Server Software: uvicorn
Server Hostname: 127.0.0.1
Server Port: 8000
Document Path: /
Document Length: 19 bytes
Concurrency Level: 1000
Time taken for tests: 1.147 seconds
Complete requests: 5000
Failed requests: 0
Total transferred: 720000 bytes
HTML transferred: 95000 bytes
Requests per second: 4359.68 [#/sec] (mean)
Time per request: 229.375 [ms] (mean)
Time per request: 0.229 [ms] (mean, across all concurrent requests)
Transfer rate: 613.08 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 20 16.3 17 70
Processing: 17 190 96.8 171 501
Waiting: 3 173 93.0 151 448
Total: 51 210 96.4 184 533
Percentage of the requests served within a certain time (ms)
50% 184
66% 209
75% 241
80% 260
90% 324
95% 476
98% 504
99% 514
100% 533 (longest request)
这篇关于架构瓶vs FastAPI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!