你有没有过这种崩溃瞬间 —— 笔记本上 uvicorn main:app --reload 跑得行云流水,接口响应快到飞起。
结果兴冲冲部署到服务器,并发量刚一上来,请求就开始排队,然后 502、504 哗啦啦地来,用户群里瞬间炸锅。
别问我怎么知道的,那种滋味,真不想你再尝一遍。
所以今天这篇,我打算把 Uvicorn、Gunicorn 这些名字绕口的家伙,用你最听得懂的方式盘一遍,再给你一套拿来就能用的部署配置。
👩💻我是爱折腾的一名程序媛,喜欢研究全栈开发的各种实践,热爱分享踩坑后的收获与思考,也享受用代码写出各种实用小工具解决问题的快乐。
如果你也在技术这条路上向前走,关注我,愿我们能彼此陪伴,一起成为更好的自己 🌱
📌 本文能帮你解决什么
🔹 搞清楚 Uvicorn、Gunicorn、WSGI、ASGI 到底谁是谁的谁
🔹 知道什么时候用 uvicorn --workers,什么时候必须上 Gunicorn
🔹 拿到生产级 Gunicorn + Uvicorn 的配置样板
🔹 避开端口冲突、超时、僵尸进程等常见大坑
🔹 学会不同环境(开发、Docker、Linux 生产)的部署套路
🧠 核心原理:别把“饭店伙计”当成“饭店老板”
很多人刚接触部署,以为 uvicorn 就是一个完整的 Web Server,其实它更像一个跑得很快的伙计,能端菜(处理请求),但不会管理后厨的人手。
咱们打个比方,你的 FastAPI 应用是一家网红餐厅:
🔹 Uvicorn
是手脚麻利的服务员,接单上菜特别快,尤其是对需要等一会的菜(异步请求),它能先去招呼别桌,不会傻站着。
🔹 Gunicorn
是店长,它自己不端盘子,但它管着一群服务员,规定谁干活、干多少活、干累了换班、出了错重启。
🔹 Nginx
是门口的大堂经理,负责迎客、分流、挡掉捣乱的人。
那 WSGI 和 ASGI 呢?就是服务员和后厨的沟通协议。
🔹 WSGI 是同步的,一次只端一个菜。
🔹 ASGI 是异步的,适合同时处理好几桌的点单,FastAPI 天生就讲 ASGI 语。
好,现在你肯定想问:那 Uvicorn 自己不是也有 --workers 参数吗?直接用不就行了?接下来重点来了。
⚡ 实战对比:直接裸奔 vs 工头带队
🐍 方案一:直接 uvicorn --workers 4
确实,Uvicorn 支持多 worker,但这个“多”是靠它自己 fork 出来的,没有 Gunicorn 那么老练。实际跑起来,你会发现:
🔹 某个 worker 悄悄死掉,它不一定能拉起来。
🔹 内存占用管理比较糙。
🔹 信号处理和优雅关机,偶尔就闹脾气。
这里有一点要特别注意,如果你在 Windows 上开发,--workers 根本不能用!
你只能老老实实开多个进程,用端口转发,巨麻烦。所以 Windows 上只建议跑单 worker 做调试。
https://pastebin.com/DQbHmppZ
https://pastebin.com/Eag2u7tD
https://pastebin.com/PNz1e6wC
https://pastebin.com/hSrnZDqj
https://pastebin.com/FFrPGgvZ
https://pastebin.com/zRDx2KuF
https://pastebin.com/CDWHrfjt
https://pastebin.com/pmTQw4M2
https://pastebin.com/xiKkWJzm
https://pastebin.com/fQGevSiF
https://pastebin.com/EreA9NKS
https://pastebin.com/AiYRhL7Y
https://pastebin.com/WHc3KAW8
https://pastebin.com/aw20U6vB
https://pastebin.com/mDBzNbcs
https://pastebin.com/YeGH9z5S
https://pastebin.com/99nc1SP1
https://pastebin.com/SWUVmHM7
https://pastebin.com/rTK2H3Dm
https://pastebin.com/pGQBZMqa
https://pastebin.com/7ZDVPYMp
https://pastebin.com/BrWxkk4g
https://pastebin.com/Ch1Q4rWL
https://pastebin.com/NQM59Cyg
https://pastebin.com/pyb1drcw
https://pastebin.com/HXsa7izw
https://pastebin.com/Rp8uZma4
https://pastebin.com/XYsv5zVb
https://pastebin.com/aF4KaziD
https://pastebin.com/H97ptUPA
https://pastebin.com/MJErQDRt
https://pastebin.com/pFw12jvU
https://pastebin.com/DbHvz3xX
https://pastebin.com/M9AD0GMY
https://pastebin.com/TekwuBkz
https://pastebin.com/pBwHYn88
https://pastebin.com/8wLpygeZ
https://pastebin.com/DEQyxsLB
https://pastebin.com/5RbTr4pg
https://pastebin.com/cQ52SAcj
https://pastebin.com/YvpRUNmv
https://pastebin.com/icqDLPM3
https://pastebin.com/UtbwTHHF
https://pastebin.com/yF2CGtQb
https://pastebin.com/34asd4tL
https://pastebin.com/PD61WZsH
https://pastebin.com/DW0xJEbd
https://pastebin.com/Xh4PVwJH
https://pastebin.com/FKj7AJn7
https://pastebin.com/rKKTVNf9
https://pastebin.com/VFAQaaeC
https://pastebin.com/3a6c2y0u
https://pastebin.com/EpYXxtgh
https://pastebin.com/NBPUsz7M
https://pastebin.com/hc2jXuBx
https://pastebin.com/gEZar887
https://pastebin.com/1QSZ3vcJ
https://pastebin.com/Qc1RLqeE
https://pastebin.com/Z8rLxe0z
https://pastebin.com/xue7XnKh
https://pastebin.com/QKWpnVjd
https://pastebin.com/xeAQqtdW
https://pastebin.com/1xF6PKEi
https://pastebin.com/yWH731Qm
https://pastebin.com/nmbYfDQr
https://pastebin.com/gA64S87M
https://pastebin.com/fzkUCQjP
https://pastebin.com/HcdQRSNy
https://pastebin.com/BndhDQPt
https://pastebin.com/LQh1Buug
https://pastebin.com/bgPdPZQJ
https://pastebin.com/42vGR0pw
https://pastebin.com/9Rf36rGW
https://pastebin.com/MhrjLziz
https://pastebin.com/R6Xi1rEQ
https://pastebin.com/Yce3XwEp
https://pastebin.com/G25tF9g0
https://pastebin.com/Fr3SjBia
https://pastebin.com/t9k39B0s
https://pastebin.com/cY3r7zYq
https://pastebin.com/Erpsk5Sb
https://pastebin.com/dWNZ2p9G
https://pastebin.com/VBuSHdF9
https://pastebin.com/YccgPHCe
https://pastebin.com/Q13AtDZ2
https://pastebin.com/tde02Y5k
https://pastebin.com/amkRKgnc
https://pastebin.com/G4djrAr9
https://pastebin.com/s7RAMNiP
https://pastebin.com/h9pgW5Eu
https://pastebin.com/bPj76aVU
https://pastebin.com/Cvw26x4S
https://pastebin.com/1MUJZud7
https://pastebin.com/VvYVU7Sf
https://pastebin.com/YRCfK6hK
https://pastebin.com/izzGWphA
https://pastebin.com/rch8k4Yq
https://pastebin.com/7F7HWX8R
https://pastebin.com/e9QthygA
https://pastebin.com/rgQ4xJNi
https://pastebin.com/3CRZLfrp
https://pastebin.com/kLNk0hgc
https://pastebin.com/JbH31fkY
https://pastebin.com/gF8AuNHm
https://pastebin.com/MeX8KYtP
https://pastebin.com/5VM66sC0
https://pastebin.com/cnWre2qF
https://pastebin.com/BBGrtA8D
https://pastebin.com/mUPCUEH3
https://pastebin.com/GV6s7mkp
https://pastebin.com/MJP3vX9u
https://pastebin.com/wA73zkMB
https://pastebin.com/UMtrMadi
https://pastebin.com/8nzXUDbm
https://pastebin.com/Agt0Cbzg
https://pastebin.com/nkgXVHKn
https://pastebin.com/eWKLGqPQ
https://pastebin.com/s7ZgLStd
https://pastebin.com/361RNhmT
https://pastebin.com/Xm0EphHk
https://pastebin.com/PpHrK20U
https://pastebin.com/6dqv2fCE
https://pastebin.com/7yK0J6X5
https://pastebin.com/epH2jKqC
https://pastebin.com/Pfb7na1P
https://pastebin.com/x7v5hFYK
https://pastebin.com/mU4Kzpna
https://pastebin.com/ggBpaJux
https://pastebin.com/EamDY4jq
https://pastebin.com/P1Hz48Vz
https://pastebin.com/Cs77MjC6
https://pastebin.com/apfAkjXY
https://pastebin.com/LfUypFXS
https://pastebin.com/FPkAfe0K
https://pastebin.com/tcXuFTRt
https://pastebin.com/1tFDcbbv
https://pastebin.com/6SQRvfHh
https://pastebin.com/LcKxgprw
https://pastebin.com/Fy8h1YVL
https://pastebin.com/Uc828MRa
https://pastebin.com/tExef980
https://pastebin.com/dRZWuPHW
https://pastebin.com/nVgaNvhA
https://pastebin.com/xPJSk1p8
https://pastebin.com/J7YQbkBr
https://pastebin.com/1vBbGerT
https://pastebin.com/6hiqsA81
https://pastebin.com/4x2bjcEi
https://pastebin.com/5py3Pe68
https://pastebin.com/Yc2cnHjH
https://pastebin.com/6PucBFNx
https://pastebin.com/Ps4MR7Gs
https://pastebin.com/a2dxTM4G
https://pastebin.com/7pAYspML
https://pastebin.com/RZiSLDCT
https://pastebin.com/wxSQXStA
https://pastebin.com/wnKkuABV
https://pastebin.com/SZLRYdwg
https://pastebin.com/vhJyva86
https://pastebin.com/fYi7yXw1
https://pastebin.com/jAPfuuPL
https://pastebin.com/W97gSuJh
https://pastebin.com/c5HXhr1G
https://pastebin.com/Phw7QVf6
https://pastebin.com/8w2uivPj
https://pastebin.com/y2VBL4gE
https://pastebin.com/wnt2DAy7
https://pastebin.com/ntNaP66i
https://pastebin.com/i7rQ4Y6D
https://pastebin.com/yumT5vvQ
https://pastebin.com/g2cVgzMQ
https://pastebin.com/Yqy5B1jG
https://pastebin.com/4SsB4SX6
https://pastebin.com/gb5FSWJJ
https://pastebin.com/9zKFwLBn
https://pastebin.com/n7d1W0RN
https://pastebin.com/HXrJVPWG
https://pastebin.com/qJvgzidx
https://pastebin.com/1CHJZWgq
https://pastebin.com/FB8Xvbh4
https://pastebin.com/3zMU6WYb
https://pastebin.com/Ahjry926
https://pastebin.com/7ZYErdEc
https://pastebin.com/w2PpeGU9
https://pastebin.com/DjKjaxHL
https://pastebin.com/0xTr2MUJ
https://pastebin.com/aM2TvPRX
https://pastebin.com/KBi4uFRA
https://pastebin.com/gUNjCnRC
https://pastebin.com/ESLrQX1U
https://pastebin.com/CDbSaCgp
https://pastebin.com/5KVN8s5E
https://pastebin.com/aRDaWMVb
https://pastebin.com/mQYL12B9
https://pastebin.com/Bmqq4Q5v
https://pastebin.com/w8cf2qUV
https://pastebin.com/3wYR9D7A
https://pastebin.com/Y0TRpucz
https://pastebin.com/dqcUmvKw
https://pastebin.com/BFixiPKY
https://pastebin.com/nfwvgBxy
https://pastebin.com/iJBCGXeE
https://pastebin.com/fSmJCWKS
https://pastebin.com/HmuucFjh
https://pastebin.com/4RrTzR3X
https://pastebin.com/qpp5gv51
https://pastebin.com/tizp9sBU
https://pastebin.com/TE4qHGSv
https://pastebin.com/965Eu7qK
https://pastebin.com/5uCQ2Ktt
https://pastebin.com/sKjgsyRJ
https://pastebin.com/tm9Wme2r
https://pastebin.com/WcnFSLC3
https://pastebin.com/98Fm7nCt
https://pastebin.com/6MqPdj1a
https://pastebin.com/Vz0LSFF1
https://pastebin.com/kSq51YVJ
https://pastebin.com/v61Wx0ku
https://pastebin.com/DdYezzAZ
https://pastebin.com/Tnjj4LuB
https://pastebin.com/RpVLDcWk
https://pastebin.com/wrDbBN4K
https://pastebin.com/JpaQCqTB
https://pastebin.com/w92QyFwU
https://pastebin.com/bHcy91v3
https://pastebin.com/Vhm4VCN3
https://pastebin.com/wFkPQExm
https://pastebin.com/RAFZHxMU
https://pastebin.com/3npAu3xV
https://pastebin.com/R0Eg0dw4
https://pastebin.com/ANunfnR1
https://pastebin.com/C59weCgY
https://pastebin.com/Ht9YV9An
https://pastebin.com/p8q0cgrR
https://pastebin.com/51Ne7irH
https://pastebin.com/WM2Fr8Aa
https://pastebin.com/dNGfCpn1
https://pastebin.com/pBXX0RxM
https://pastebin.com/VCW91FwH
https://pastebin.com/RaBGcKLL
https://pastebin.com/xKVBVnjt
https://pastebin.com/8HJyxCNX
https://pastebin.com/0uAQqtJp
https://pastebin.com/MnTm5Swe
https://pastebin.com/x2sYqGuv
https://pastebin.com/TCcfwqd8
https://pastebin.com/iqmqC0DL
https://pastebin.com/eDizZbah
https://pastebin.com/rc4HEbTT
https://pastebin.com/EDve04HS
https://pastebin.com/pC01HEQA
https://pastebin.com/978PetqK
https://pastebin.com/RQ7BLurQ
https://pastebin.com/t1apm6J5
https://pastebin.com/yrGS9E2P
https://pastebin.com/vRn3zmrm
https://pastebin.com/k2xteh0E
https://pastebin.com/uGg6a4pH
https://pastebin.com/t8FVz67M
https://pastebin.com/AChzpNxp
https://pastebin.com/ZBLzt98A
https://pastebin.com/4ZJTW5G6
https://pastebin.com/M2CQ849h
https://pastebin.com/5ZX9sSeZ
https://pastebin.com/Azmxhxrr
https://pastebin.com/ajVek4bZ
https://pastebin.com/V68FrkJ8
https://pastebin.com/net9GPkv
https://pastebin.com/kNtby5FC
https://pastebin.com/5fyzK6tb
https://pastebin.com/qCbk81gr
https://pastebin.com/Jj5y3VxK
https://pastebin.com/4ggAPhK1
https://pastebin.com/LFWuLjTB
https://pastebin.com/UAvGnCFZ
https://pastebin.com/3QH30J0s
https://pastebin.com/9RTTGM7F
https://pastebin.com/kbnGTfKK
https://pastebin.com/KDrS7a9c
https://pastebin.com/prRx3upi
https://pastebin.com/GY1sR4gX
https://pastebin.com/ta1urHpe
https://pastebin.com/7rseZAZu
https://pastebin.com/3U6MD32s
https://pastebin.com/ACRtgT9E
https://pastebin.com/YkgQAgsd
https://pastebin.com/EbLKfepf
https://pastebin.com/gA4hJa95
https://pastebin.com/YLB4EmJd
https://pastebin.com/DfcXNMtd
https://pastebin.com/qxbPL7jW
https://pastebin.com/zVYDV9G0
https://pastebin.com/1LEv8A8J
https://pastebin.com/WWesXcC9
https://pastebin.com/gtspdA0N
https://pastebin.com/2zP5usd8
https://pastebin.com/djY4CQ5E
https://pastebin.com/9rxkBt2a
https://pastebin.com/swRBb25a
https://pastebin.com/cjfx8wuX
https://pastebin.com/ANfsWijd
https://pastebin.com/uRgYVrAp
https://pastebin.com/0ew3LF2D
https://pastebin.com/BnptL6ia
https://pastebin.com/rwSxJtZS
https://pastebin.com/fGDy0yJv
https://pastebin.com/ZRFmc1eD
https://pastebin.com/Qg0bT1rC
https://pastebin.com/EQqfbF7A
https://pastebin.com/a46mH8fp
https://pastebin.com/Ks2cjSNz
https://pastebin.com/nUUnKs9M
https://pastebin.com/rbz3i3kG
https://pastebin.com/eCEX9HfV
https://pastebin.com/EK05BCLR
https://pastebin.com/CSXXWZB4
https://pastebin.com/UjnP4C43
https://pastebin.com/isjs4cw9
https://pastebin.com/MF6Ttmkj
https://pastebin.com/5SAVfy6t
https://pastebin.com/1Bq1tNEd
https://pastebin.com/htgaVQR6
https://pastebin.com/bw5Z09nK
https://pastebin.com/9WTcQf4V
https://pastebin.com/tWPv7hfD
https://pastebin.com/hcmdWazQ
https://pastebin.com/Rsu26V17
https://pastebin.com/3UhMsmqZ
https://pastebin.com/zNHSjGGh
https://pastebin.com/7XVTf0db
https://pastebin.com/74PhNmyk
https://pastebin.com/TSkvrJd2
3 个评论
要回复文章请先登录或注册
不知道的事i (作者)
不知道的事i (作者)
不知道的事i (作者)