V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
huichao
V2EX  ›  Python

请教大家, flask 适不适合做 2k+ endpoint 的后台 api 项目?

  •  
  •   huichao · 2020-12-18 09:58:23 +08:00 · 2407 次点击
    这是一个创建于 1217 天前的主题,其中的信息可能已经有所发展或是发生改变。
    手上有个大点儿的项目,大概估算有 2k+ 的 api endpoint, 在想着用 flask 来做整个后台的 api,可是之前用 flask 只是做小的项目而已,和考虑到 flask 的性能,想请教 flask 到底适不适合做 2k+ endpoint 的 api 项目?敬请回复,讨论技术并参考建议。
    10 条回复    2020-12-21 12:32:39 +08:00
    cian
        1
    cian  
       2020-12-20 09:52:06 +08:00
    api 可以 group,不同 group 跑在不同 flask 实例上
    cian
        2
    cian  
       2020-12-20 09:52:40 +08:00
    其实就是拆分成多个子系统了
    ErwinCheung
        3
    ErwinCheung  
       2020-12-20 12:57:50 +08:00
    很赞的分享 学习了
    iConnect
        4
    iConnect  
       2020-12-20 17:44:16 +08:00 via Android
    担心性能用 fastapi
    feiandxs
        5
    feiandxs  
       2020-12-20 23:17:06 +08:00
    如果只是 endpoint 数量多点,那还没到性能部分。
    事实上,机器内存别太小,就够。endpoint 这种东西,在你 python main.py 跑起来 flask 的时候,该加载加载,没问题了,进内存了。剩下具体的逻辑反正都是执行的时候才跑,没什么性能问题。

    但 endpoint 多了,我仍然建议拆成多个系统……多个 flask 也没问题啊,毕竟 flask 写东西真的又快又好……
    abersheeran
        6
    abersheeran  
       2020-12-21 09:23:13 +08:00
    巧了,两千+的路由,关注度很高的 Python web 框架都会因为路由太多,导致单次访问速度显著下降。今年 PyCon China 我去讲过。其中,flask 的 ASGI 版本,最拉跨……如果你非要在几个高 star 框架里选,还是 fastapi 吧。如果不考虑这方面,可以试试 https://github.com/abersheeran/index.py
    huichao
        7
    huichao  
    OP
       2020-12-21 10:48:55 +08:00
    @abersheeran 谢谢你的建议。路由多了会影响性能 😨 , 这点儿我有点儿害怕了, 那 fastapi 不会吗?
    rogwan
        8
    rogwan  
       2020-12-21 11:26:02 +08:00
    @abersheeran 2K+性能显著下降的原因,是路由的正则性能拉跨导致的吗?还是其他什么原因造成的?
    abersheeran
        9
    abersheeran  
       2020-12-21 12:27:21 +08:00   ❤️ 1
    @huichao fastapi 也会……但是比 flask 快一点,也更现代化。

    @rogwan 嗯,它们的路由都是遍历正则匹配。flask 、django 、fastapi 、bottle 、responder,都是这样。
    abersheeran
        10
    abersheeran  
       2020-12-21 12:32:39 +08:00
    @huichao 你这种两千+路由的情况不妨试试 https://github.com/abersheeran/index.py 作者就是我,有 bug 随时提 issue 。或者你用其他的框架的时候,把一个项目根据 prefix 拆分成多个,用 nginx 之类的前置代理进行分流,可以减缓这方面带来的影响。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2007 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 56ms · UTC 00:01 · PVG 08:01 · LAX 17:01 · JFK 20:01
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.