{"id":204686,"date":"2025-05-29T10:28:24","date_gmt":"2025-05-29T02:28:24","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204686\/"},"modified":"2025-05-29T10:28:24","modified_gmt":"2025-05-29T02:28:24","slug":"python-%e5%b9%b6%e8%a1%8c%e8%bf%90%e8%a1%8c%e5%bf%85%e9%a1%bb%e5%9c%a8-main-%e5%87%bd%e6%95%b0%e4%b8%ad%e5%90%97%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204686\/","title":{"rendered":"Python \u5e76\u884c\u8fd0\u884c\u5fc5\u987b\u5728 main \u51fd\u6570\u4e2d\u5417\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Python \u5e76\u884c\u8fd0\u884c\u5fc5\u987b\u5728 main \u51fd\u6570\u4e2d\u5417\uff1f<\/h1>\n<p>\u672c\u7bc7\u6587\u7ae0\u4e3b\u8981\u662f\u7ed3\u5408\u6211\u4e4b\u524d\u9762\u8bd5\u7684\u5404\u79cd\u7ecf\u5386\u548c\u5b9e\u6218\u5f00\u53d1\u4e2d\u9047\u5230\u7684\u95ee\u9898\u89e3\u51b3\u7ecf\u9a8c\u6574\u7406\u7684\uff0c\u5e0c\u671b\u8fd9\u7bc7\u300aPython \u5e76\u884c\u8fd0\u884c\u5fc5\u987b\u5728 main \u51fd\u6570\u4e2d\u5417\uff1f\u300b\u5bf9\u4f60\u6709\u5f88\u5927\u5e2e\u52a9\uff01\u6b22\u8fce\u6536\u85cf\uff0c\u5206\u4eab\u7ed9\u66f4\u591a\u7684\u9700\u8981\u7684\u670b\u53cb\u5b66\u4e60~<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241113\/17314754756734381380517.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>Python\u5e76\u884c\u8fd0\u884c\u4e0d\u5c40\u9650\u4e8emain\u51fd\u6570<\/strong><\/p>\n<p>\u5f53\u4f7f\u7528Python\u7684multiprocessing.Pool\u8fdb\u884c\u8fdb\u7a0b\u5e76\u884c\u8fd0\u884c\u65f6\uff0c\u901a\u5e38\u4f1a\u5c06\u4ee3\u7801\u653e\u7f6e\u5728__name__==&#8221;__main__&#8221;\u6761\u4ef6\u5185\u3002\u7136\u800c\uff0c\u5982\u679c\u4e0d\u5e0c\u671b\u5c06\u4ee3\u7801\u9650\u5236\u5728\u6b64\u5904\uff0c\u53ef\u4ee5\u5728\u6a21\u5757\u6216\u51fd\u6570\u4e2d\u6267\u884c\u5e76\u884c\u8fd0\u884c\u3002<\/p>\n<p><strong>\u539f\u56e0<\/strong><\/p>\n<p>\u5b98\u7f51\u6307\u51fa\uff0c\u5f53\u4f7f\u7528ProcessPoolExecutor\u65f6\uff0c\u5fc5\u987b\u5728__name__==&#8221;__main__&#8221;\u6761\u4ef6\u4e0b\u8fd0\u884c\u3002\u8fd9\u662f\u56e0\u4e3amultiprocessing\u4f7f\u7528fork\u65b9\u6cd5\u521b\u5efa\u8fdb\u7a0b\uff0c\u800cfork\u4ec5\u9002\u7528\u4e8e\u4e3b\u8fdb\u7a0b\u3002<\/p>\n<p><strong>\u66ff\u4ee3\u65b9\u6cd5<\/strong><\/p>\n<p>\u4ee5\u4e0b\u65b9\u6cd5\u53ef\u7528\u4e8e\u5728\u4e0d\u4f7f\u7528main\u51fd\u6570\u7684\u60c5\u51b5\u4e0b\u6267\u884c\u5e76\u884c\u8fd0\u884c\uff1a<\/p>\n<ul>\n<li><strong>\u4f7f\u7528ThreadPoolExecutor\uff1a<\/strong>ThreadPoolExecutor\u4f7f\u7528\u7ebf\u7a0b\u6c60\u800c\u4e0d\u662f\u8fdb\u7a0b\uff0c\u56e0\u6b64\u4e0d\u9700\u8981__name__==&#8221;__main__&#8221;\u6761\u4ef6\u3002<\/li>\n<li><strong>\u5728Linux\u7cfb\u7edf\u4e0a\u4f7f\u7528spawn\uff1a<\/strong>spawn\u65b9\u6cd5\u521b\u5efa\u8fdb\u7a0b\u9002\u7528\u4e8eLinux\u7cfb\u7edf\uff0c\u5e76\u4e14\u4e0d\u4f7f\u7528fork\u3002\u56e0\u6b64\uff0c\u5728Linux\u7cfb\u7edf\u4e0a\uff0c\u53ef\u4ee5\u5c06\u5e76\u884c\u8fd0\u884c\u4ee3\u7801\u653e\u5728\u6a21\u5757\u4e2d\u3002<\/li>\n<li><strong>\u53c2\u8003stackoverflow\u7b54\u6848\uff1a<\/strong>\u8be5\u7b54\u6848\u63d0\u4f9b\u4e86\u9488\u5bf9\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u7684\u5177\u4f53\u89e3\u51b3\u65b9\u6848\u3002<\/li>\n<\/ul>\n<p><strong>\u8865\u5145<\/strong><\/p>\n<p>\u5982\u679c\u4f7f\u7528spawn\u65b9\u6cd5\uff0c\u5219\u4f1a\u68c0\u67e5\u662f\u5426\u5728__name__==&#8221;__main__&#8221;\u6761\u4ef6\u4e0b\u8fd0\u884c\u3002\u5982\u679c\u4e0d\u6ee1\u8db3\u6b64\u6761\u4ef6\uff0c\u5219\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u8fdb\u7a0b\uff0c\u5e76\u4e14getattr(process.current_process(), &#8216;_inheriting&#8217;, False)\u7684\u503c\u4e3aTrue\u3002<\/p>\n<p>\u901a\u8fc7\u7406\u89e3\u8fd9\u4e9b\u673a\u5236\uff0c\u5f00\u53d1\u4eba\u5458\u53ef\u4ee5\u5728\u4e0d\u5c40\u9650\u4e8emain\u51fd\u6570\u7684\u60c5\u51b5\u4e0b\u5229\u7528Python\u8fdb\u884c\u5e76\u884c\u8fd0\u884c\u3002<\/p>\n<p>\u7406\u8bba\u8981\u638c\u63e1\uff0c\u5b9e\u64cd\u4e0d\u80fd\u843d\uff01\u4ee5\u4e0a\u5173\u4e8e\u300aPython \u5e76\u884c\u8fd0\u884c\u5fc5\u987b\u5728 main \u51fd\u6570\u4e2d\u5417\uff1f\u300b\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u5927\u5bb6\u90fd\u638c\u63e1\u4e86\u5427\uff01\u5982\u679c\u60f3\u8981\u7ee7\u7eed\u63d0\u5347\u81ea\u5df1\u7684\u80fd\u529b\uff0c\u90a3\u4e48\u5c31\u6765\u5173\u6ce8\u516c\u4f17\u53f7\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python \u5e76\u884c\u8fd0\u884c\u5fc5\u987b\u5728 m&#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-204686","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204686","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=204686"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204686\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204686"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204686"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204686"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}