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
LeeReamond
V2EX  ›  Python

Fastapi web 应用生产级守护部署的方式?

  •  
  •   LeeReamond · 2023-03-10 14:05:54 +08:00 · 2407 次点击
    这是一个创建于 385 天前的主题,其中的信息可能已经有所发展或是发生改变。

    多进程 fastapi ,一直用的是跑分网的部署方法,用 gunicorn 挂 uvicorn worker 启动。倒是没出过什么问题,因为我们以前压力一直不大,未来有点预期要上压力,我想起来这个好像有个坑来着。因为印象里以前看哪个大佬提过一嘴说 gunicorn 实际上只能启动但是进程挂了之后连重拉起功能都没有的,也不知道是不是属实。。。

    19 条回复    2023-03-24 14:50:22 +08:00
    zoranz
        1
    zoranz  
       2023-03-10 17:47:54 +08:00
    你有啥场景能把进程干爆啊
    idblife
        2
    idblife  
       2023-03-10 17:54:34 +08:00
    supervisord
    paopjian
        3
    paopjian  
       2023-03-10 19:29:54 +08:00
    那就拿 supervisor 保护呗
    echo1937
        4
    echo1937  
       2023-03-10 20:44:09 +08:00
    gunicorn 是 WSGI HTTP Server ,很类似 Java 里的 Tomcat ; Supervisor 则是 A Process Control System ,负责 monitor and control a number of processes ,你的进程挂了会帮你重新拉起来。以上都是他们官方文档摘抄的原文。
    neoblackcap
        5
    neoblackcap  
       2023-03-10 20:53:52 +08:00
    现在而言,配置一下 systemd 就能守护好进程
    LeeReamond
        6
    LeeReamond  
    OP
       2023-03-10 20:56:56 +08:00
    @neoblackcap
    @paopjian
    @idblife 一个疑问,gunicorn 是有子进程的,supervisor 只能监控主进程啊,子进程挂了怎么办
    lgh
        7
    lgh  
       2023-03-11 08:21:44 +08:00 via iPhone
    子进程挂了会重启啊。不过我的是纯 gunicorn ,并没有下挂 uvicorn 。
    ClericPy
        8
    ClericPy  
       2023-03-11 10:07:08 +08:00
    Fastapi 以前自己服务器上就 uvicorn 直接起, 反正更新代码重启下就够了, 不开 --reload

    公司服务器上也是 gunicorn + uvicorn, 主要用到 gunicorn 那个 HUP 信号无中断的热重启, 守护 systemd 就够了, supervisord 这么多年了也不知道当初那些小 bug 好了没有

    现在在考虑 docker 那边怎么无中断热重启, 想丢 K8S 里面, 之前想的是 nginx 帮我转发多个端口的, 重启的时候一个个重启自动切流量, 不知道有什么更好办法没有. docker 里面走 gunicorn 就算接到 HUP, 但是代码是跟镜像走的...
    julyclyde
        9
    julyclyde  
       2023-03-11 11:18:55 +08:00
    systemd 是唯一正确答案
    supervisord 自己由谁守护还是个问题呢
    SKYNE
        10
    SKYNE  
       2023-03-11 13:48:09 +08:00
    容器化丢 k8s 里
    LeeReamond
        11
    LeeReamond  
    OP
       2023-03-11 16:52:41 +08:00
    @ClericPy 可能确实容器化+nginx 分流是比较合理的方案。寻思了一下,似乎直接 gunicorn 实现多节点也只有一行代码启动的优势,毕竟通信都靠中间服务了,可能细研究连方便也未必方便。
    ClericPy
        12
    ClericPy  
       2023-03-11 21:04:33 +08:00
    @LeeReamond nginx 我也是没办法, 但凡组里有个运维就研究研究 Istio 什么的了. 我就想知道个当前的版本答案, gunicorn 这套五年前就不流行了
    LeeReamond
        13
    LeeReamond  
    OP
       2023-03-11 21:41:32 +08:00
    @ClericPy 哈哈,老哥版本答案总结到位
    fcfangcc
        14
    fcfangcc  
       2023-03-13 17:06:14 +08:00
    gunicorn 子进程挂了是会自动拉起的,不用担心
    luzihang
        15
    luzihang  
       2023-03-14 09:07:11 +08:00
    pm2 也可以用用,比 supervisord 好看,好用
    ClericPy
        16
    ClericPy  
       2023-03-14 18:54:50 +08:00
    @LeeReamond 大哥, 我是来等新版本云原生的答案的...
    byaiu
        17
    byaiu  
       2023-03-16 17:40:25 +08:00
    @ClericPy 求问当下流行什么?直接 systemd 接管?
    ClericPy
        18
    ClericPy  
       2023-03-16 18:46:47 +08:00
    @byaiu
    流行云原生吧, 主要想抄个版本答案, 能达到和以前那套一样流量不丢失的就行. 我也求问啊... 现在想到的就是多实例切流量, 不然默认的实例重启会弄丢当前连接

    不是专业后端, 所以想照抄个当下答案, 如果不是后端是脚本, 就随便重启了
    ykb8121
        19
    ykb8121  
       2023-03-24 14:50:22 +08:00
    systemd
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2808 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 13:30 · PVG 21:30 · LAX 06:30 · JFK 09:30
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.