{"id":49679,"date":"2024-12-03T16:32:37","date_gmt":"2024-12-03T08:32:37","guid":{"rendered":"https:\/\/fwq.ai\/blog\/49679\/"},"modified":"2024-12-03T16:32:37","modified_gmt":"2024-12-03T08:32:37","slug":"%e5%a4%9a%e8%bf%9b%e7%a8%8b%e4%b8%ad%e4%bd%bf%e7%94%a8-join-%e6%96%b9%e6%b3%95%ef%bc%8c%e4%b8%bb%e8%bf%9b%e7%a8%8b%e4%bb%a3%e7%a0%81%e6%98%af%e5%90%a6%e4%bc%9a%e5%9c%a8%e5%ad%90%e8%bf%9b%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/49679\/","title":{"rendered":"\u591a\u8fdb\u7a0b\u4e2d\u4f7f\u7528 join \u65b9\u6cd5\uff0c\u4e3b\u8fdb\u7a0b\u4ee3\u7801\u662f\u5426\u4f1a\u5728\u5b50\u8fdb\u7a0b\u672a\u5b8c\u6210\u524d\u6267\u884c\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u591a\u8fdb\u7a0b\u4e2d\u4f7f\u7528 join \u65b9\u6cd5\uff0c\u4e3b\u8fdb\u7a0b\u4ee3\u7801\u662f\u5426\u4f1a\u5728\u5b50\u8fdb\u7a0b\u672a\u5b8c\u6210\u524d\u6267\u884c\uff1f<\/h1>\n<p>\u4ece\u73b0\u5728\u5f00\u59cb\uff0c\u6211\u4eec\u8981\u52aa\u529b\u5b66\u4e60\u5566\uff01\u4eca\u5929\u6211\u7ed9\u5927\u5bb6\u5e26\u6765\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u8bf7\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427\uff01\u4e0b\u6587\u4e2d\u7684\u5185\u5bb9\u6211\u4eec\u4e3b\u8981\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u5728\u9605\u8bfb\u672c\u6587\u8fc7\u7a0b\u4e2d\u6709\u9047\u5230\u4e0d\u6e05\u695a\u7684\u5730\u65b9\uff0c\u6b22\u8fce\u7559\u8a00\u5440\uff01\u6211\u4eec\u4e00\u8d77\u8ba8\u8bba\uff0c\u4e00\u8d77\u5b66\u4e60\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241031\/173037927267237e080e64f.jpg\" class=\"aligncenter\" title=\"\u591a\u8fdb\u7a0b\u4e2d\u4f7f\u7528 join \u65b9\u6cd5\uff0c\u4e3b\u8fdb\u7a0b\u4ee3\u7801\u662f\u5426\u4f1a\u5728\u5b50\u8fdb\u7a0b\u672a\u5b8c\u6210\u524d\u6267\u884c\uff1f\u63d2\u56fe\" alt=\"\u591a\u8fdb\u7a0b\u4e2d\u4f7f\u7528 join \u65b9\u6cd5\uff0c\u4e3b\u8fdb\u7a0b\u4ee3\u7801\u662f\u5426\u4f1a\u5728\u5b50\u8fdb\u7a0b\u672a\u5b8c\u6210\u524d\u6267\u884c\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u591a\u8fdb\u7a0b\u4e2d\u7684 for \u5faa\u73af join \u95ee\u9898<\/strong><\/p>\n<p><strong>\u95ee\u9898\uff1a<\/strong><\/p>\n<p>\u5728\u4f7f\u7528\u591a\u8fdb\u7a0b\u65f6\uff0c\u5728 for \u5faa\u73af\u4e2d\u4f7f\u7528 join \u65b9\u6cd5\u662f\u5426\u53ef\u80fd\u5bfc\u81f4\u4e3b\u8fdb\u7a0b\u4ee3\u7801\u5728\u67d0\u4e2a\u5b50\u8fdb\u7a0b\u5c1a\u672a\u5b8c\u6210\u540e\u6267\u884c\uff1f<\/p>\n<p><strong>\u4ee3\u7801\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>import os\nfrom multiprocessing import Process\n\ndef func(num):\n    print(f'in func', num, os.getpid(), os.getppid())\n\nif __name__ == '__main__':\n    print(f'in main', os.getpid(), os.getppid())\n    p_l = []\n    for i in range(10):\n        p = Process(target=func, args=(i,))\n        p.start()  # \u975e\u963b\u585e\uff0c\u521b\u5efa\u5b50\u8fdb\u7a0b\n        p_l.append(p)\n    print(p_l)\n    for p in p_l:\n        p.join()  # \u963b\u585e\uff0c\u76f4\u81f3\u5b50\u8fdb\u7a0b\u6267\u884c\u5b8c\u6bd5\n    print('\u4e3b\u8fdb\u7a0b \u7684 \u4ee3\u7801\u6267\u884c\u7ed3\u675f\u4e86')<\/pre>\n<p><strong>\u7b54\u6848\uff1a<\/strong><\/p>\n<p>\u4e0d\u53ef\u80fd\u3002<\/p>\n<p>join \u65b9\u6cd5\u4f1a\u963b\u585e\u4e3b\u8fdb\u7a0b\uff0c\u76f4\u5230\u5b50\u8fdb\u7a0b\u6267\u884c\u5b8c\u6bd5\u540e\u624d\u7ee7\u7eed\u6267\u884c\u3002\u56e0\u6b64\uff0c\u4e3b\u8fdb\u7a0b\u7684\u4ee3\u7801\uff0c\u5305\u62ec print(&#8216;\u4e3b\u8fdb\u7a0b \u7684 \u4ee3\u7801\u6267\u884c\u7ed3\u675f\u4e86&#8217;)\uff0c\u53ea\u4f1a\u5728\u6240\u6709\u5b50\u8fdb\u7a0b\u90fd\u5b8c\u6210\u540e\u624d\u6267\u884c\u3002<\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300a\u591a\u8fdb\u7a0b\u4e2d\u4f7f\u7528 join \u65b9\u6cd5\uff0c\u4e3b\u8fdb\u7a0b\u4ee3\u7801\u662f\u5426\u4f1a\u5728\u5b50\u8fdb\u7a0b\u672a\u5b8c\u6210\u524d\u6267\u884c\uff1f\u300b\u7684\u5185\u5bb9\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\uff0c\u5982\u679c\u6709\u4ec0\u4e48\u7591\u95ee\u6216\u8005\u5efa\u8bae\uff0c\u53ef\u4ee5\u5728\u7c73\u4e91\u516c\u4f17\u53f7\u4e0b\u591a\u591a\u56de\u590d\u4ea4\u6d41\uff1b\u6587\u4e2d\u82e5\u6709\u4e0d\u6b63\u4e4b\u5904\uff0c\u4e5f\u5e0c\u671b\u56de\u590d\u7559\u8a00\u4ee5\u544a\u77e5\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u591a\u8fdb\u7a0b\u4e2d\u4f7f\u7528 join \u65b9\u6cd5\uff0c\u4e3b\u8fdb\u7a0b\u4ee3\u7801\u662f\u5426\u4f1a\u5728\u5b50\u8fdb\u7a0b\u672a\u5b8c\u6210\u524d\u6267\u884c\uff1f \u4ece\u73b0\u5728\u5f00\u59cb\uff0c\u6211\u4eec\u8981\u52aa\u529b\u5b66\u4e60\u5566\uff01\u4eca\u5929\u6211\u7ed9\u5927\u5bb6\u5e26\u6765\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u8bf7\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427\uff01\u4e0b\u6587\u4e2d\u7684\u5185\u5bb9\u6211\u4eec\u4e3b\u8981\u4f1a\u6d89\u53ca\u5230\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u5728\u9605\u8bfb\u672c\u6587\u8fc7\u7a0b\u4e2d\u6709\u9047\u5230\u4e0d\u6e05\u695a\u7684\u5730\u65b9\uff0c\u6b22\u8fce\u7559\u8a00\u5440\uff01\u6211\u4eec\u4e00\u8d77\u8ba8\u8bba\uff0c\u4e00\u8d77\u5b66\u4e60\uff01 \u591a\u8fdb\u7a0b\u4e2d\u7684 for \u5faa\u73af join \u95ee\u9898 \u95ee\u9898\uff1a \u5728\u4f7f\u7528\u591a\u8fdb\u7a0b\u65f6\uff0c\u5728 for \u5faa\u73af\u4e2d\u4f7f\u7528 join \u65b9\u6cd5\u662f\u5426\u53ef\u80fd\u5bfc\u81f4\u4e3b\u8fdb\u7a0b\u4ee3\u7801\u5728\u67d0\u4e2a\u5b50\u8fdb\u7a0b\u5c1a\u672a\u5b8c\u6210\u540e\u6267\u884c\uff1f \u4ee3\u7801\u793a\u4f8b\uff1a import os from multiprocessing import Process def func(num): print(f&#8217;in func&#8217;, num, os.getpid(), os.getppid()) if __name__ == &#8216;__main__&#8217;: print(f&#8217;in main&#8217;, os.getpid(), os.getppid()) p_l = [] for i in range(10): p = Process(target=func, args=(i,)) p.start() # \u975e\u963b\u585e\uff0c\u521b\u5efa\u5b50\u8fdb\u7a0b p_l.append(p) print(p_l) for p [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-49679","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/49679","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=49679"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/49679\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=49679"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=49679"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=49679"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}