{"id":204997,"date":"2025-05-29T14:00:50","date_gmt":"2025-05-29T06:00:50","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204997\/"},"modified":"2025-05-29T14:00:50","modified_gmt":"2025-05-29T06:00:50","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8%e8%bf%9b%e7%a8%8b%e6%b1%a0%e4%b8%ad%e5%88%9b%e5%bb%ba%e5%ad%90%e8%bf%9b%e7%a8%8b%e4%bb%a5%e6%89%a7%e8%a1%8c%e5%a4%9a%e4%bb%bb%e5%8a%a1%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204997\/","title":{"rendered":"\u5982\u4f55\u5728\u8fdb\u7a0b\u6c60\u4e2d\u521b\u5efa\u5b50\u8fdb\u7a0b\u4ee5\u6267\u884c\u591a\u4efb\u52a1\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u5728\u8fdb\u7a0b\u6c60\u4e2d\u521b\u5efa\u5b50\u8fdb\u7a0b\u4ee5\u6267\u884c\u591a\u4efb\u52a1\uff1f <\/h1>\n<p>\u6700\u8fd1\u53d1\u73b0\u4e0d\u5c11\u5c0f\u4f19\u4f34\u90fd\u5bf9\u5f88\u611f\u5174\u8da3\uff0c\u6240\u4ee5\u4eca\u5929\u7ee7\u7eed\u7ed9\u5927\u5bb6\u4ecb\u7ecd<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u7684\u77e5\u8bc6\uff0c\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u5728\u8fdb\u7a0b\u6c60\u4e2d\u521b\u5efa\u5b50\u8fdb\u7a0b\u4ee5\u6267\u884c\u591a\u4efb\u52a1\uff1f \u300b<\/span>\u4e3b\u8981\u5185\u5bb9\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u80fd\u5e2e\u5230\u4f60\uff01\u5f53\u7136\u5982\u679c\u9605\u8bfb\u672c\u6587\u65f6\u5b58\u5728\u4e0d\u540c\u60f3\u6cd5\uff0c\u53ef\u4ee5\u5728\u8bc4\u8bba\u4e2d\u8868\u8fbe\uff0c\u4f46\u662f\u8bf7\u52ff\u4f7f\u7528\u8fc7\u6fc0\u7684\u63aa\u8f9e~<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241121\/1732160564673eac3461800.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u8fdb\u7a0b\u6c60\u4e2d\u4f7f\u7528\u5b50\u8fdb\u7a0b<\/strong><\/p>\n<p>\u5728\u591a\u4efb\u52a1\u5904\u7406\u4e2d\uff0c\u9762\u5bf9\u5927\u91cf\u4efb\u52a1\u9700\u8981\u5904\u7406\u65f6\uff0c\u8fdb\u7a0b\u6c60\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u9009\u62e9\uff0c\u56e0\u4e3a\u5b83\u907f\u514d\u4e86\u6700\u5927\u8fdb\u7a0b\u6570\u91cf\u9650\u5236\u3002\u4f46\u662f\uff0c\u5728\u67d0\u4e9b\u573a\u666f\u4e0b\uff0c\u9700\u8981\u5b50\u8fdb\u7a0b\u521b\u5efa\u5b50\u8fdb\u7a0b\u3002\u5982\u4f55\u5728\u8fdb\u7a0b\u6c60\u4e2d\u5b9e\u73b0\u8fd9\u4e00\u70b9\u5462\uff1f<\/p>\n<p><strong>\u95ee\u9898\u89e3\u7b54<\/strong><\/p>\n<p>\u8981\u7406\u89e3\u5982\u4f55\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u9996\u5148\u9700\u8981\u4e86\u89e3\u8fdb\u7a0b\u6c60\u7684\u5de5\u4f5c\u539f\u7406\u3002\u8fdb\u7a0b\u6c60\u4f7f\u7528\u51e0\u4e2a\u9884\u5148\u521b\u5efa\u7684 worker \u8fdb\u7a0b\u6765\u6267\u884c\u4efb\u52a1\u3002\u8fd9\u4e9b worker \u8fdb\u7a0b\u662f\u8fdb\u7a0b\u6c60\u521b\u5efa\u5e76\u7ba1\u7406\u7684\u3002\u95ee\u9898\u6240\u5728\u662f\u56e0\u4e3a\u8fdb\u7a0b\u6c60\u7684 worker \u8fdb\u7a0b\u4e0d\u80fd\u518d\u521b\u5efa\u5b50\u8fdb\u7a0b\u3002<\/p>\n<p>\u4f46\u662f\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u6765\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff1a<\/p>\n<ol>\n<li>\n<p><strong>\u521b\u5efa\u4e0e\u8fdb\u7a0b\u6c60\u7684 worker \u8fdb\u7a0b\u6570\u91cf\u76f8\u540c\u7684\u5b50\u8fdb\u7a0b\uff1a<\/strong><\/p>\n<ul>\n<li>\u5728\u8fdb\u7a0b\u6c60\u5916\u521b\u5efa\u5b50\u8fdb\u7a0b\u3002<\/li>\n<li>\u5728\u8fd9\u4e9b\u5b50\u8fdb\u7a0b\u4e2d\uff0c\u521b\u5efa worker \u8fdb\u7a0b\u7684\u6570\u91cf\u4e0e\u8fdb\u7a0b\u6c60\u4e2d\u76f8\u540c\u3002<\/li>\n<li>\u5728 worker \u8fdb\u7a0b\u4e2d\uff0c\u521b\u5efa\u5b50\u8fdb\u7a0b\u6765\u6267\u884c\u4efb\u52a1\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p>\u4e0b\u56fe\u5c55\u793a\u4e86\u8fd9\u79cd\u65b9\u6cd5\uff1a<\/p>\n<p>[\u56fe\u7247\uff1a\u8fdb\u7a0b\u6c60\u4e2d\u4f7f\u7528\u5b50\u8fdb\u7a0b\u7684\u6d41\u7a0b\u56fe]<\/p>\n<ol>\n<li>\n<p><strong>\u4ee3\u7801\u5b9e\u73b0\uff1a<\/strong><\/p>\n<pre>import multiprocessing\nimport time\n\n\ndef print_run(msg):\n print('msg.%s' % msg)\n time.sleep(2)\n\n\ndef create_child_process():\n # \u521b\u5efa\u591a\u4e2a\u5b50\u8fdb\u7a0b\uff0c\u6bcf\u4e2a\u5b50\u8fdb\u7a0b\u90fd\u521b\u5efa worker \u8fdb\u7a0b\n child_processes = []\n for i in range(10):\n     child = multiprocessing.Process(target=print_run, args=(str(i),))\n     child.start()\n     child_processes.append(child)\n\n # \u7b49\u5f85\u5b50\u8fdb\u7a0b\u5b8c\u6210\n for child in child_processes:\n     child.join()\n\n\ndef set_multprocess():\n # \u521b\u5efa\u8fdb\u7a0b\u6c60\n pool = multiprocessing.Pool(processes=10)\n\n # \u521b\u5efa\u5b50\u8fdb\u7a0b\uff0c\u5728\u5b50\u8fdb\u7a0b\u4e2d\u521b\u5efa worker \u8fdb\u7a0b\n create_child_process()\n\n # \u5173\u95ed\u8fdb\u7a0b\u6c60\n pool.close()\n pool.join()\n\n print('finished')\n\n\nif __name__ == '__main__':\n set_multprocess()<\/pre>\n<\/li>\n<\/ol>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u901a\u8fc7\u5c06\u5b50\u8fdb\u7a0b\u7684\u521b\u5efa\u4e0e\u6267\u884c\u4efb\u52a1\u5206\u5f00\uff0c\u89e3\u51b3\u4e86\u5728\u8fdb\u7a0b\u6c60\u4e2d\u4f7f\u7528\u5b50\u8fdb\u7a0b\u7684\u95ee\u9898\u3002<\/p>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300a\u5982\u4f55\u5728\u8fdb\u7a0b\u6c60\u4e2d\u521b\u5efa\u5b50\u8fdb\u7a0b\u4ee5\u6267\u884c\u591a\u4efb\u52a1\uff1f \u300b\u7684\u5185\u5bb9\u4e86\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u57fa\u7840\u77e5\u8bc6\u7684\u5b66\u4e60\u548c\u5de9\u56fa\uff0c\u662f\u4e3a\u4e86\u66f4\u597d\u7684\u5c06\u5176\u8fd0\u7528\u5230\u9879\u76ee\u4e2d\uff0c\u6b22\u8fce\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u5e26\u4f60\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u77e5\u8bc6\u70b9\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u5728\u8fdb\u7a0b\u6c60\u4e2d\u521b\u5efa\u5b50\u8fdb\u7a0b\u4ee5\u6267\u884c\u591a&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-204997","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204997","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=204997"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204997\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204997"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204997"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204997"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}