{"id":15,"date":"2023-07-15T13:40:03","date_gmt":"2023-07-15T05:40:03","guid":{"rendered":"http:\/\/www.weigot.com\/?p=15"},"modified":"2023-07-15T13:40:03","modified_gmt":"2023-07-15T05:40:03","slug":"flask-%e5%ba%94%e7%94%a8%e7%a8%8b%e5%ba%8f%e9%9c%80%e8%a6%81%e5%90%8c%e6%97%b6%e5%a4%84%e7%90%86%e5%a4%9a%e4%b8%aa-post-%e8%af%b7%e6%b1%82","status":"publish","type":"post","link":"http:\/\/www.weigot.com\/index.php\/2023\/07\/15\/flask-%e5%ba%94%e7%94%a8%e7%a8%8b%e5%ba%8f%e9%9c%80%e8%a6%81%e5%90%8c%e6%97%b6%e5%a4%84%e7%90%86%e5%a4%9a%e4%b8%aa-post-%e8%af%b7%e6%b1%82\/","title":{"rendered":"Flask \u5e94\u7528\u7a0b\u5e8f\u9700\u8981\u540c\u65f6\u5904\u7406\u591a\u4e2a POST \u8bf7\u6c42"},"content":{"rendered":"\n<p>\u5982\u679c\u60a8\u7684 Flask \u5e94\u7528\u7a0b\u5e8f\u9700\u8981\u540c\u65f6\u5904\u7406\u591a\u4e2a POST \u8bf7\u6c42\uff0c\u5e76\u4e14\u6bcf\u4e2a\u8bf7\u6c42\u9700\u8981\u8fd0\u884c\u76f8\u540c\u7684 Python \u4efb\u52a1\uff0c\u60a8\u53ef\u4ee5\u8003\u8651\u4f7f\u7528 Python \u7684&nbsp;<code>concurrent.futures<\/code>&nbsp;\u6a21\u5757\u6765\u5b9e\u73b0\u5e76\u53d1\u5904\u7406\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u6f14\u793a\u5982\u4f55\u4f7f\u7528&nbsp;<code>concurrent.futures<\/code>&nbsp;\u6a21\u5757\u5728 Flask \u5e94\u7528\u7a0b\u5e8f\u4e2d\u540c\u65f6\u8fd0\u884c\u591a\u4e2a Python \u4efb\u52a1\uff1a<\/p>\n\n\n\n<p>pythonCopy<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import time\nfrom flask import Flask, request\nfrom concurrent.futures import ThreadPoolExecutor\n\napp = Flask(__name__)\nexecutor = ThreadPoolExecutor(max_workers=4)\n\n@app.route('\/process_post', methods=&#91;'POST'])\ndef process_post():\n    # \u83b7\u53d6 POST \u8bf7\u6c42\u4e2d\u7684\u6570\u636e\n    data = request.get_data()\n\n    # \u5728\u7ebf\u7a0b\u6c60\u4e2d\u5f02\u6b65\u6267\u884c Python \u4efb\u52a1\n    future = executor.submit(long_running_task, data)\n\n    # \u8fd4\u56de\u4f20\u9012\u6210\u529f\u7684\u4fe1\u606f\n    return \"Data transmitted successfully. Task submitted.\"\n\ndef long_running_task(data):\n    # \u5904\u7406\u6570\u636e\n    # ...\n\n    # \u6a21\u62df\u957f\u65f6\u95f4\u8fd0\u884c\u7684\u4efb\u52a1\n    time.sleep(10)\n\n    # \u8fd4\u56de\u7ed3\u679c\n    return \"Data processed successfully.\"\n<\/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\u51fd\u6570\uff0c\u7528\u4e8e\u5904\u7406\u6570\u636e\u5e76\u6a21\u62df\u957f\u65f6\u95f4\u8fd0\u884c\u7684\u4efb\u52a1\u3002\u6211\u4eec\u4f7f\u7528 Python \u7684&nbsp;<code>concurrent.futures.ThreadPoolExecutor<\/code>&nbsp;\u7c7b\u521b\u5efa\u4e00\u4e2a\u7ebf\u7a0b\u6c60\uff0c\u5e76\u5c06\u6570\u636e\u4f20\u9012\u7ed9&nbsp;<code>long_running_task()<\/code>&nbsp;\u51fd\u6570\u8fdb\u884c\u5904\u7406\u3002\u6bcf\u4e2a\u4efb\u52a1\u5c06\u5f02\u6b65\u6267\u884c\uff0c\u5e76\u5728\u5b8c\u6210\u540e\u8fd4\u56de\u5904\u7406\u7ed3\u679c\u3002<\/p>\n\n\n\n<p>\u5728 Flask \u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u8def\u7531\u51fd\u6570&nbsp;<code>process_post()<\/code>&nbsp;\u6765\u63a5\u6536 POST \u8bf7\u6c42\uff0c\u5e76\u5c06\u4efb\u52a1\u63d0\u4ea4\u5230\u7ebf\u7a0b\u6c60\u4e2d\u3002\u6211\u4eec\u4f7f\u7528&nbsp;<code>executor.submit()<\/code>&nbsp;\u65b9\u6cd5\u6765\u5f02\u6b65\u63d0\u4ea4\u4efb\u52a1\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a&nbsp;<code>concurrent.futures.Future<\/code>&nbsp;\u5bf9\u8c61\uff0c\u8868\u793a\u4efb\u52a1\u5df2\u7ecf\u63d0\u4ea4\u6210\u529f\u3002\u6700\u540e\uff0c\u6211\u4eec\u8fd4\u56de\u4f20\u9012\u6210\u529f\u7684\u4fe1\u606f\u3002<\/p>\n\n\n\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5982\u679c\u60a8\u7684 Flask \u5e94\u7528\u7a0b\u5e8f\u9700\u8981\u5904\u7406\u5927\u91cf\u7684 POST \u8bf7\u6c42\uff0c\u5e76\u4e14\u6bcf\u4e2a\u8bf7\u6c42\u9700\u8981\u8fd0\u884c\u76f8\u540c\u7684 Python \u4efb\u52a1\uff0c\u60a8\u5e94\u8be5\u6839\u636e\u9700\u8981\u8c03\u6574\u7ebf\u7a0b\u6c60\u7684\u5927\u5c0f\uff0c\u5e76\u786e\u4fdd\u670d\u52a1\u5668\u6709\u8db3\u591f\u7684\u8d44\u6e90\u6765\u5904\u7406\u8fd9\u4e9b\u4efb\u52a1\u3002\u5982\u679c\u60a8\u7684 Python \u4efb\u52a1\u662f CPU \u5bc6\u96c6\u578b\u7684\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u4f7f\u7528\u591a\u4e2a\u8fdb\u7a0b\u6216\u4f7f\u7528\u5f02\u6b65\u4efb\u52a1\u961f\u5217\u6765\u8fdb\u884c\u5904\u7406\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u679c\u60a8\u7684 Flask \u5e94\u7528\u7a0b\u5e8f\u9700\u8981\u540c\u65f6\u5904\u7406\u591a\u4e2a POST \u8bf7\u6c42\uff0c\u5e76\u4e14\u6bcf\u4e2a\u8bf7\u6c42\u9700\u8981\u8fd0\u884c\u76f8\u540c\u7684 Python \u4efb\u52a1 [&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-15","post","type-post","status-publish","format-standard","hentry","category-suibianjilu"],"_links":{"self":[{"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/posts\/15","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=15"}],"version-history":[{"count":1,"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/posts\/15\/revisions"}],"predecessor-version":[{"id":16,"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/posts\/15\/revisions\/16"}],"wp:attachment":[{"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/media?parent=15"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/categories?post=15"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/tags?post=15"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}