{"id":13,"date":"2023-07-15T13:16:47","date_gmt":"2023-07-15T05:16:47","guid":{"rendered":"http:\/\/www.weigot.com\/?p=13"},"modified":"2023-07-15T13:16:47","modified_gmt":"2023-07-15T05:16:47","slug":"python-%e5%90%8e%e5%8f%b0%e4%bb%bb%e5%8a%a1%e6%89%a7%e8%a1%8c%e6%97%b6%e9%97%b4%e5%be%88%e9%95%bf%ef%bc%8c%e5%8f%af%e4%bb%a5%e4%bd%bf%e7%94%a8-flask-%e7%9a%84%e5%bc%82%e6%ad%a5%e5%a4%84%e7%90%86","status":"publish","type":"post","link":"http:\/\/www.weigot.com\/index.php\/2023\/07\/15\/python-%e5%90%8e%e5%8f%b0%e4%bb%bb%e5%8a%a1%e6%89%a7%e8%a1%8c%e6%97%b6%e9%97%b4%e5%be%88%e9%95%bf%ef%bc%8c%e5%8f%af%e4%bb%a5%e4%bd%bf%e7%94%a8-flask-%e7%9a%84%e5%bc%82%e6%ad%a5%e5%a4%84%e7%90%86\/","title":{"rendered":"Python \u540e\u53f0\u4efb\u52a1\u6267\u884c\u65f6\u95f4\u5f88\u957f\uff0c\u53ef\u4ee5\u4f7f\u7528 Flask \u7684\u5f02\u6b65\u5904\u7406"},"content":{"rendered":"\n<p>\u5982\u679c\u60a8\u7684 Python \u540e\u53f0\u4efb\u52a1\u6267\u884c\u65f6\u95f4\u5f88\u957f\uff0c\u53ef\u4ee5\u4f7f\u7528 Flask \u7684\u5f02\u6b65\u5904\u7406\u529f\u80fd\u6765\u8fd4\u56de\u4f20\u9012\u6210\u529f\u7684\u4fe1\u606f\uff0c\u540c\u65f6\u5728\u540e\u53f0\u6267\u884c\u957f\u65f6\u95f4\u8fd0\u884c\u7684\u4efb\u52a1\u3002<\/p>\n\n\n\n<p>\u4f7f\u7528 Flask \u7684\u5f02\u6b65\u5904\u7406\u529f\u80fd\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5176\u4e2d\u4e00\u79cd\u662f\u4f7f\u7528 Celery \u4f5c\u4e3a\u4efb\u52a1\u961f\u5217\u3002Celery \u662f\u4e00\u4e2a Python \u5206\u5e03\u5f0f\u4efb\u52a1\u961f\u5217\uff0c\u53ef\u4ee5\u7528\u4e8e\u5f02\u6b65\u5904\u7406\u957f\u65f6\u95f4\u8fd0\u884c\u7684\u4efb\u52a1\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528 Celery \u7684\u793a\u4f8b\uff1a<\/p>\n\n\n\n<p>\u5b89\u88c5 Celery\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pip install celery<\/code><\/pre>\n\n\n\n<p>\u5728 Flask \u5e94\u7528\u7a0b\u5e8f\u4e2d\u914d\u7f6e Celery\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># app.py\r\nfrom flask import Flask\r\nfrom celery import Celery\r\n\r\napp = Flask(__name__)\r\napp.config&#91;'CELERY_BROKER_URL'] = 'redis:\/\/localhost:6379\/0'\r\napp.config&#91;'CELERY_RESULT_BACKEND'] = 'redis:\/\/localhost:6379\/0'\r\n\r\ncelery = Celery(app.name, broker=app.config&#91;'CELERY_BROKER_URL'])\r\ncelery.conf.update(app.config)<\/code><\/pre>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 Redis \u4f5c\u4e3a Celery \u7684\u6d88\u606f\u4ee3\u7406\u548c\u7ed3\u679c\u540e\u7aef\u3002\u60a8\u53ef\u4ee5\u6839\u636e\u9700\u8981\u66f4\u6539\u8fd9\u4e9b\u8bbe\u7f6e\u3002<\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"3\">\n<li>\u5b9a\u4e49&nbsp;<a>Celery<\/a>&nbsp;\u4efb\u52a1\uff1a<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code># tasks.py\r\nfrom celery import Celery\r\n\r\ncelery = Celery('tasks', broker='redis:\/\/localhost:6379\/0')\r\n\r\n@celery.task\r\ndef long_running_task(data):\r\n    # \u5904\u7406\u6570\u636e\r\n    # ...\r\n\r\n    # \u8fd4\u56de\u7ed3\u679c\r\n    return \"Data processed successfully.\"<\/code><\/pre>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a&nbsp;<code>long_running_task()<\/code>&nbsp;\u7684 Celery \u4efb\u52a1\u3002\u8be5\u4efb\u52a1\u63a5\u6536\u4e00\u4e2a\u53c2\u6570&nbsp;<code>data<\/code>\uff0c\u7528\u4e8e\u5904\u7406\u6570\u636e\u5e76\u8fd4\u56de\u5904\u7406\u7ed3\u679c\u3002<\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"4\">\n<li>\u5728 Flask \u5e94\u7528\u7a0b\u5e8f\u4e2d\u8c03\u7528 Celery \u4efb\u52a1\uff1a<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code># app.py\r\nfrom flask import Flask, request\r\nfrom tasks import long_running_task\r\n\r\n@app.route('\/process_post', methods=&#91;'POST'])\r\ndef process_post():\r\n    # \u83b7\u53d6 POST \u8bf7\u6c42\u4e2d\u7684\u6570\u636e\r\n    data = request.get_data()\r\n\r\n    # \u8c03\u7528 Celery \u4efb\u52a1\r\n    task = long_running_task.delay(data)\r\n\r\n    # \u8fd4\u56de\u4f20\u9012\u6210\u529f\u7684\u4fe1\u606f\r\n    return \"Data transmitted successfully. Task ID: {}\".format(task.id)<\/code><\/pre>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5728 Flask \u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684\u8def\u7531\u51fd\u6570\u4e2d\u8c03\u7528\u4e86 Celery \u4efb\u52a1&nbsp;<code>long_running_task()<\/code>\u3002\u6211\u4eec\u4f7f\u7528&nbsp;<code>delay()<\/code>&nbsp;\u65b9\u6cd5\u6765\u5f02\u6b65\u8c03\u7528\u4efb\u52a1\uff0c\u5e76\u5c06 POST \u6570\u636e\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u4efb\u52a1\u3002\u6700\u540e\uff0c\u6211\u4eec\u8fd4\u56de\u4f20\u9012\u6210\u529f\u7684\u4fe1\u606f\uff0c\u5e76\u5305\u542b Celery \u4efb\u52a1\u7684 ID\u3002<\/p>\n\n\n\n<p>\u8fd9\u6837\u505a\u7684\u597d\u5904\u662f\uff0c\u5728\u8c03\u7528 Celery \u4efb\u52a1\u65f6\uff0cFlask \u5e94\u7528\u7a0b\u5e8f\u5c06\u7acb\u5373\u8fd4\u56de\u4f20\u9012\u6210\u529f\u7684\u4fe1\u606f\uff0c\u5e76\u5728\u540e\u53f0\u5f02\u6b65\u6267\u884c\u957f\u65f6\u95f4\u8fd0\u884c\u7684\u4efb\u52a1\u3002\u5f53\u4efb\u52a1\u5b8c\u6210\u65f6\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 Celery \u7684\u7ed3\u679c\u540e\u7aef\u6765\u83b7\u53d6\u4efb\u52a1\u7684\u7ed3\u679c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u679c\u60a8\u7684 Python \u540e\u53f0\u4efb\u52a1\u6267\u884c\u65f6\u95f4\u5f88\u957f\uff0c\u53ef\u4ee5\u4f7f\u7528 Flask \u7684\u5f02\u6b65\u5904\u7406\u529f\u80fd\u6765\u8fd4\u56de\u4f20\u9012\u6210\u529f\u7684\u4fe1\u606f\uff0c\u540c\u65f6\u5728 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-13","post","type-post","status-publish","format-standard","hentry","category-suibianjilu"],"_links":{"self":[{"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/posts\/13","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/comments?post=13"}],"version-history":[{"count":1,"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/posts\/13\/revisions"}],"predecessor-version":[{"id":14,"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/posts\/13\/revisions\/14"}],"wp:attachment":[{"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/media?parent=13"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/categories?post=13"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/tags?post=13"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}