{"id":191,"date":"2026-02-27T19:22:02","date_gmt":"2026-02-27T11:22:02","guid":{"rendered":"https:\/\/www.handytoo.com\/?p=191"},"modified":"2026-03-10T10:28:57","modified_gmt":"2026-03-10T02:28:57","slug":"flask-%e9%a1%b9%e7%9b%ae%e7%94%a8-gunicorn-%e9%83%a8%e7%bd%b2%ef%bc%8c%e5%91%8a%e5%88%ab%e6%8b%89%e8%b7%a8%e7%9a%84app-run","status":"publish","type":"post","link":"https:\/\/www.handytoo.com\/?p=191","title":{"rendered":"Flask \u9879\u76ee\u7528 Gunicorn \u90e8\u7f72\uff0c\u544a\u522b\u62c9\u8de8\u7684app.run()"},"content":{"rendered":"\n<p>Flask \u81ea\u5e26\u7684\u5f00\u53d1\u670d\u52a1\u5668\uff08\u5c31\u662f\u90a3\u4e2a\u00a0<code>app.run()<\/code>\uff09\u53ea\u914d\u5728\u672c\u5730\u8dd1\u8dd1\uff0c\u771f\u4e0a\u4e86\u751f\u4ea7\u73af\u5883\uff0c\u5206\u5206\u949f\u7ed9\u4f60\u989c\u8272\u770b\u2014\u2014\u5355\u8fdb\u7a0b\u3001\u5bb9\u6613\u5d29\u3001\u625b\u4e0d\u4f4f\u5e76\u53d1\u3002\u5b98\u65b9\u6587\u6863\u767d\u7eb8\u9ed1\u5b57\u5199\u7740\uff1a\u8fd9\u73a9\u610f\u513f\u4e0d\u662f\u8ba9\u4f60\u90e8\u7f72\u7528\u7684\u3002\u6240\u4ee5\u5f97\u6362\u4e2a\u6b63\u7ecf\u7684 WSGI \u670d\u52a1\u5668\u3002Gunicorn \u662f Python \u5708\u5b50\u91cc\u6700\u987a\u624b\u7684\u65b9\u6848\u4e4b\u4e00\uff0c\u914d\u7f6e\u7b80\u5355\uff0c\u6587\u6863\u5168\uff0c\u793e\u533a\u91cc\u968f\u4fbf\u4e00\u641c\u5168\u662f\u6848\u4f8b\u3002\u6211\u5728\u8fd9\u7bc7\u6587\u7ae0\u91cc\u5c31\u8ddf\u4f60\u634b\u4e00\u904d\uff0c\u4ece\u96f6\u5f00\u59cb\u628a Gunicorn \u88c5\u8fdb\u4f60\u7684 Flask \u9879\u76ee\uff0c\u987a\u4fbf\u628a\u90a3\u4e9b\u5bb9\u6613\u8e29\u7684\u5751\u90fd\u6307\u51fa\u6765\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u5b89\u88c5 Gunicorn\uff0c\u5c31\u8fd9\u4e48\u7b80\u5355<\/h3>\n\n\n\n<p>\u5047\u8bbe\u4f60\u5df2\u7ecf\u6709\u4e86\u4e00\u4e2a Flask \u9879\u76ee\uff0c\u5165\u53e3\u6587\u4ef6\u53eb&nbsp;<code>app.py<\/code>\uff0c\u91cc\u9762\u6709\u4e2a Flask \u5b9e\u4f8b&nbsp;<code>app = Flask(__name__)<\/code>\u3002\u5148\u88c5 Gunicorn\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">pip install gunicorn<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">\u5982\u679c\u7528\u4e86\u865a\u62df\u73af\u5883\uff0c\u8bb0\u5f97\u8fdb\u865a\u62df\u73af\u5883\u518d\u88c5\u3002\u6ca1\u5565\u6280\u672f\u542b\u91cf\uff0c\u4e0b\u4e00\u6b65\u3002\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u8dd1\u8d77\u6765\u770b\u770b\u6548\u679c<\/h3>\n\n\n\n<p>\u5728\u9879\u76ee\u6839\u76ee\u5f55\u4e0b\u6267\u884c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">gunicorn -w 4 app:app<\/pre>\n\n\n\n<p><code>app:app<\/code>&nbsp;\u7684\u610f\u601d\u662f\u201c\u4ece app.py \u91cc\u627e\u540d\u53eb app \u7684\u90a3\u4e2a Flask \u5b9e\u4f8b\u201d\uff0c<code>-w 4<\/code>&nbsp;\u8868\u793a\u5f00 4 \u4e2a\u5de5\u4f5c\u8fdb\u7a0b\u3002\u9ed8\u8ba4\u7ed1\u5728&nbsp;<code>127.0.0.1:8000<\/code>\uff0c\u8fd9\u65f6\u5019\u4f60\u53ea\u80fd\u4ece\u672c\u673a\u8bbf\u95ee\uff08curl localhost:8000\uff09\uff0c\u6d4f\u89c8\u5668\u8bbf\u95ee\u4e0d\u4e86\uff0c\u5f97\u914d Nginx \u505a\u53cd\u5411\u4ee3\u7406\u3002<\/p>\n\n\n\n<p>\u60f3\u4e34\u65f6\u6d4b\u8bd5\u5916\u7f51\u80fd\u4e0d\u80fd\u901a\uff0c\u53ef\u4ee5\u6539\u6210\u76d1\u542c\u6240\u6709\u7f51\u5361\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">gunicorn -w 4 -b 0.0.0.0:8000 app:app<\/pre>\n\n\n\n<p>\u4e0d\u8fc7\u751f\u4ea7\u73af\u5883\u522b\u8fd9\u4e48\u5e72\u2014\u2014\u76f4\u63a5\u628a Flask \u66b4\u9732\u51fa\u53bb\u4e0d\u5b89\u5168\uff0c\u8ba9 Nginx \u5728\u524d\u9762\u6321\u7740\u624d\u662f\u6b63\u7ecf\u505a\u6cd5\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. worker \u6570\u91cf\u600e\u4e48\u5b9a\uff1f\u6709\u516c\u5f0f\u4e5f\u6709\u7ecf\u9a8c<\/h2>\n\n\n\n<p><code>-w<\/code>\u00a0\u540e\u9762\u8ddf\u7684\u6570\u5b57\u51b3\u5b9a\u540c\u65f6\u80fd\u5904\u7406\u591a\u5c11\u4e2a\u8bf7\u6c42\u3002\u7f51\u4e0a\u6709\u4e2a\u516c\u5f0f\u00a0<code>(2 x CPU\u6838\u5fc3\u6570) + 1<\/code>\uff0c\u4f46\u522b\u6b7b\u78d5\u3002\u6bcf\u4e2a worker \u90fd\u662f\u4e00\u4e2a\u72ec\u7acb\u7684 Python \u8fdb\u7a0b\uff0c\u633a\u5403\u5185\u5b58\u7684\u3002\u6211\u4e00\u822c\u5148\u5f00 4 \u4e2a\uff0c\u7528\u00a0<code>htop<\/code>\u00a0\u76ef\u7740\u5185\u5b58\uff0c\u591f\u7528\u5c31\u5f80\u4e0a\u52a0\uff0c\u4e0d\u591f\u5c31\u51cf\u3002\u5982\u679c\u4f60\u7684\u5e94\u7528\u5927\u91cf\u6d89\u53ca I\/O\uff08\u6bd4\u5982\u8c03\u5916\u90e8 API\u3001\u8bfb\u5199\u6570\u636e\u5e93\uff09\uff0c\u53ef\u4ee5\u8003\u8651\u6362 worker \u7c7b\u578b\uff0c\u6bd4\u5982\u00a0<code>-k gevent<\/code>\uff0c\u80fd\u5927\u5e45\u63d0\u5347\u5e76\u53d1\u80fd\u529b\u3002\u4f46\u8981\u662f\u666e\u901a CRUD \u9879\u76ee\uff0c\u9ed8\u8ba4\u7684 sync worker \u8db3\u591f\u4e86\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. \u8ba9\u5b83\u53d8\u6210\u7cfb\u7edf\u670d\u52a1\uff0c\u5d29\u4e86\u81ea\u52a8\u91cd\u542f<\/h2>\n\n\n\n<p>\u5149\u5728\u7ec8\u7aef\u91cc\u8dd1\u4e0d\u662f\u957f\u4e45\u4e4b\u8ba1\u2014\u2014\u4e07\u4e00\u4f60\u9000\u51fa\u767b\u5f55\uff0c\u6216\u8005\u7ec8\u7aef\u5173\u4e86\uff0cGunicorn \u4e5f\u5c31\u6302\u4e86\u3002\u5f97\u7528 systemd \u628a\u5b83\u7ba1\u8d77\u6765\u3002<\/p>\n\n\n\n<p>\u5728&nbsp;<code>\/etc\/systemd\/system\/<\/code>&nbsp;\u4e0b\u65b0\u5efa\u4e00\u4e2a\u6587\u4ef6\uff0c\u6bd4\u5982&nbsp;<code>myflask.service<\/code>\uff0c\u5185\u5bb9\u5927\u6982\u957f\u8fd9\u6837\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[Unit]\nDescription=Gunicorn instance for My Flask App\nAfter=network.target\n\n[Service]\nUser=www-data\nGroup=www-data\nWorkingDirectory=\/\u4f60\u7684\u9879\u76ee\u7edd\u5bf9\u8def\u5f84\nEnvironment=\"PATH=\/\u4f60\u7684\u865a\u62df\u73af\u5883\u8def\u5f84\/bin\"\nExecStart=\/\u4f60\u7684\u865a\u62df\u73af\u5883\u8def\u5f84\/bin\/gunicorn -w 4 -b 127.0.0.1:8000 app:app\nRestart=always\n\n[Install]\nWantedBy=multi-user.target<\/pre>\n\n\n\n<p>\u5199\u5b8c\u4fdd\u5b58\uff0c\u7136\u540e\u6267\u884c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo systemctl daemon-reload\nsudo systemctl start myflask\nsudo systemctl enable myflask   <em># \u5f00\u673a\u81ea\u542f<\/em><\/pre>\n\n\n\n<p>\u4e4b\u540e\u7528&nbsp;<code>systemctl status myflask<\/code>&nbsp;\u770b\u72b6\u6001\uff0c<code>journalctl -u myflask<\/code>&nbsp;\u770b\u65e5\u5fd7\u3002\u518d\u4e5f\u4e0d\u7528\u62c5\u5fc3\u8fdb\u7a0b\u610f\u5916\u9000\u51fa\u4e86\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">5. \u642d\u914d Nginx\uff0c\u8ba9\u670d\u52a1\u66f4\u5b89\u5168<\/h2>\n\n\n\n<p>Gunicorn \u53ea\u8d1f\u8d23\u8dd1 Python \u4ee3\u7801\uff0c\u9759\u6001\u6587\u4ef6\u3001HTTPS\u3001\u8d1f\u8f7d\u5747\u8861\u8fd9\u4e9b\u6d3b\u513f\u5f97\u4ea4\u7ed9 Nginx\u3002Nginx \u914d\u7f6e\u91cc\u52a0\u4e00\u6bb5\u4ee3\u7406\u8f6c\u53d1\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">server {\n    listen 80;\n    server_name yourdomain.com;\n\n    location \/ {\n        proxy_pass http:\/\/127.0.0.1:8000;\n        proxy_set_header Host $host;\n        proxy_set_header X-Real-IP $remote_addr;\n        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\n    }\n\n    location \/static {\n        alias \/\u4f60\u7684\u9879\u76ee\u8def\u5f84\/static;\n    }\n}<\/pre>\n\n\n\n<p>\u8fd9\u6837\u7528\u6237\u8bbf\u95ee\u4f60\u7684\u57df\u540d\uff0cNginx \u628a\u8bf7\u6c42\u8f6c\u7ed9 Gunicorn\uff0c\u9759\u6001\u6587\u4ef6\u76f4\u63a5\u7531 Nginx \u5904\u7406\uff0c\u6548\u7387\u9ad8\u5f97\u591a\u3002\u522b\u5fd8\u4e86\u91cd\u542f Nginx\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">6. \u4e00\u70b9\u5173\u4e8e\u914d\u7f6e\u7684\u788e\u788e\u5ff5<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u7ed1\u5b9a\u5730\u5740<\/h3>\n\n\n\n<p>\u751f\u4ea7\u73af\u5883 Gunicorn \u5e94\u8be5\u53ea\u76d1\u542c&nbsp;<code>127.0.0.1<\/code>\uff0c\u8fd9\u6837\u53ea\u6709\u672c\u673a\u7684 Nginx \u80fd\u8bbf\u95ee\uff0c\u5916\u7f51\u8fdb\u4e0d\u6765\uff0c\u5b89\u5168\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u8d85\u65f6\u8bbe\u7f6e<\/h3>\n\n\n\n<p>\u5982\u679c\u4f60\u7684\u67d0\u4e2a\u63a5\u53e3\u5904\u7406\u65f6\u95f4\u7279\u522b\u957f\uff08\u6bd4\u5982\u751f\u6210\u62a5\u8868\uff09\uff0c\u53ef\u80fd\u8d85\u8fc7 Gunicorn \u9ed8\u8ba4\u7684 30 \u79d2\u8d85\u65f6\uff0c\u53ef\u4ee5\u7528&nbsp;<code>-t 120<\/code>&nbsp;\u6539\u6210 120 \u79d2\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u65e5\u5fd7\u8bb0\u5f55<\/h3>\n\n\n\n<p>Gunicorn \u9ed8\u8ba4\u628a\u8bbf\u95ee\u65e5\u5fd7\u6253\u5230 stdout\uff0c\u53ef\u4ee5\u7528&nbsp;<code>--access-logfile<\/code>&nbsp;\u6307\u5b9a\u6587\u4ef6\u8def\u5f84\uff0c\u65b9\u4fbf\u6392\u67e5\u95ee\u9898\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">7. \u8e29\u5751\u8bb0\u5f55<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u865a\u62df\u73af\u5883\u8def\u5f84\u5199\u9519<\/strong>\uff1asystemd \u91cc\u00a0<code>Environment<\/code>\u00a0\u548c\u00a0<code>ExecStart<\/code>\u00a0\u7684\u8def\u5f84\u5fc5\u987b\u7528\u7edd\u5bf9\u8def\u5f84\uff0c\u5c11\u4e00\u4e2a\u659c\u6760\u90fd\u8d77\u4e0d\u6765\u3002<\/li>\n\n\n\n<li><strong>\u7aef\u53e3\u51b2\u7a81<\/strong>\uff1a\u5982\u679c 8000 \u88ab\u522b\u7684\u7a0b\u5e8f\u5360\u4e86\uff0c\u6362\u4e00\u4e2a\u7aef\u53e3\u6216\u8005\u5148\u00a0<code>lsof -i:8000<\/code>\u00a0\u770b\u770b\u662f\u8c01\u3002<\/li>\n\n\n\n<li><strong>worker \u70b8\u5f39<\/strong>\uff1a<code>-w<\/code>\u00a0\u8bbe\u5f97\u592a\u5927\uff0c\u5185\u5b58\u76f4\u63a5\u7206\u6389\uff0c\u76d1\u63a7\u4e00\u4e0b\u5b9e\u9645\u8d1f\u8f7d\uff0c\u522b\u8d2a\u5fc3\u3002<\/li>\n\n\n\n<li><strong>\u9759\u6001\u6587\u4ef6 404<\/strong>\uff1a\u68c0\u67e5 Nginx \u7684\u00a0<code>alias<\/code>\u00a0\u8def\u5f84\uff0c\u6743\u9650\u5bf9\u4e0d\u5bf9\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">8. \u6700\u540e\u8bf4\u4e24\u53e5<\/h2>\n\n\n\n<p>Gunicorn + Nginx \u8fd9\u5957\u7ec4\u5408\uff0cPython \u5708\u91cc\u8dd1\u4e86\u597d\u591a\u5e74\uff0c\u7a33\u5f97\u5f88\u3002\u4e00\u5f00\u59cb\u914d\u8d77\u6765\u53ef\u80fd\u6709\u70b9\u61f5\uff0c\u4f46\u914d\u597d\u4e86\u4e4b\u540e\uff0c\u4e00\u4e2a\u9879\u76ee\u63a5\u7740\u4e00\u4e2a\u9879\u76ee\u590d\u7528\uff0c\u6548\u7387\u8d8a\u6765\u8d8a\u9ad8\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u4f60\u7528\u7684\u662f Docker\uff0c\u90a3\u5957\u8def\u53c8\u4e0d\u4e00\u6837\uff0c\u4f46\u6838\u5fc3\u8fd8\u662f Gunicorn\u3002\u5148\u628a\u8fd9\u4e00\u5957\u8dd1\u901a\uff0c\u518d\u5b66\u5bb9\u5668\u5316\u4e5f\u4e0d\u8fdf\u3002<\/p>\n\n\n\n<p>\u53cd\u6b63\u6211\u6bcf\u4e2a Flask \u9879\u76ee\u4e0a\u7ebf\u90fd\u662f\u8fd9\u5957\u6d41\u7a0b\uff0c\u6ca1\u51fa\u8fc7\u5927\u95ee\u9898\u3002\u4f60\u53ef\u4ee5\u6309\u6b65\u9aa4\u8bd5\u8bd5\uff0c\u9047\u5230\u95ee\u9898\u641c\u4e00\u4e0b\uff0c\u57fa\u672c\u90fd\u6709\u7b54\u6848\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Flask \u81ea\u5e26\u7684\u5f00\u53d1\u670d\u52a1\u5668\uff08\u5c31\u662f\u90a3\u4e2a\u00a0app.run()\uff09\u53ea\u914d\u5728\u672c\u5730\u8dd1\u8dd1\uff0c\u771f\u4e0a\u4e86\u751f\u4ea7\u73af\u5883\uff0c\u5206\u5206\u949f\u7ed9\u4f60\u989c\u8272\u770b\u2014 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[24],"class_list":["post-191","post","type-post","status-publish","format-standard","hentry","category-python","tag-python"],"_links":{"self":[{"href":"https:\/\/www.handytoo.com\/index.php?rest_route=\/wp\/v2\/posts\/191","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.handytoo.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.handytoo.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.handytoo.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.handytoo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=191"}],"version-history":[{"count":1,"href":"https:\/\/www.handytoo.com\/index.php?rest_route=\/wp\/v2\/posts\/191\/revisions"}],"predecessor-version":[{"id":192,"href":"https:\/\/www.handytoo.com\/index.php?rest_route=\/wp\/v2\/posts\/191\/revisions\/192"}],"wp:attachment":[{"href":"https:\/\/www.handytoo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=191"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.handytoo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=191"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.handytoo.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=191"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}