LeeReamond
V2EX  ›  Python

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

By LeeReamond at 2023 年 3 月 10 日 · 4296 次点击

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

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

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

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

不是专业后端, 所以想照抄个当下答案, 如果不是后端是脚本, 就随便重启了
ykb8121
   19
ykb8121  
   2023 年 3 月 24 日
systemd
推荐学习书目
› 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
© 2026 V2EX · 58ms · 3.9.8.5