{"id":14681,"date":"2023-03-06T22:22:13","date_gmt":"2023-03-06T14:22:13","guid":{"rendered":"http:\/\/139.9.1.231\/?p=14681"},"modified":"2023-03-10T23:00:53","modified_gmt":"2023-03-10T15:00:53","slug":"python_name___main","status":"publish","type":"post","link":"http:\/\/139.9.1.231\/index.php\/2023\/03\/06\/python_name___main\/","title":{"rendered":"Python\u7a0b\u5e8f\u5165\u53e3 __name__ == &#8216;__main__&#8217; \u6709\u91cd\u8981\u529f\u80fd\uff08\u591a\u7ebf\u7a0b\uff09\u800c\u975e\u7f16\u7a0b\u4e60\u60ef"},"content":{"rendered":"\n<p class=\"has-text-align-center has-light-pink-background-color has-background\"><em><strong>\u6458\u81ea\uff1a<a href=\"https:\/\/zhuanlan.zhihu.com\/p\/340965963\">https:\/\/zhuanlan.zhihu.com\/p\/340965963<\/a><\/strong><\/em><\/p>\n\n\n\n<p>          \u5728Python\u4e2d\uff0c\u88ab\u79f0\u4e3a\u300c\u7a0b\u5e8f\u7684\u5165\u53e3\u300d\u7684 if __name__ ==&#8217;__main__&#8217;: \u603b\u662f\u51fa\u73b0\u5728\u5404\u79cd\u793a\u4f8b\u4ee3\u7801\u4e2d\uff0c\u6709\u4e00\u79cd<strong>\u6d41\u4f20\u5e7f\u6cdb\u7684\u9519\u8bef\u89c2\u70b9\u662f\u300c\u8fd9\u53ea\u662fPython\u7684\u4e00\u79cd\u7f16\u7801\u4e60\u60ef\u300d<\/strong>\u3002\u4e8b\u5b9e\u4e0a\u7a0b\u5e8f\u7684\u5165\u53e3\u975e\u5e38\u6709\u7528\uff0c\u7edd\u975e\u53ef\u6709\u53ef\u65e0\uff0c\u4f8b\u5982\u5728Python\u81ea\u5e26\u7684\u591a\u7ebf\u7a0b\u5e93\u8981\u6c42\u5fc5\u987b\u628a\u4e3b\u8fdb\u7a0b\u5199\u5728 if\u5165\u53e3\u5185\u90e8\u624d\u80fd\u6b63\u5e38\u8fd0\u884c\u3002<\/p>\n\n\n\n<p>\u76f4\u63a5\u5199\u5728Python\u6700\u5de6\u7aef\u6ca1\u6709\u7f29\u8fdb\u7684\u4ee3\u7801\uff0c\u5728\u8fd9\u4e2a *.py \u6587\u4ef6\u88ab\u76f4\u63a5\u8fd0\u884c\u3001\u6216\u8005\u88ab\u8c03\u7528\u65f6\u4f1a\u88ab\u6267\u884c\uff0c\u53ea\u6709<strong>\u5199\u5728 if __name__ ==&#8217;__main__&#8217;:&nbsp;<\/strong>if\u5165\u53e3\u5185\u90e8\u624d\u4e0d\u4f1a\u5728\u88ab\u8c03\u7528\u65f6\u6267\u884c\u3002Python\u7528\u8fd9\u4e2a\u7b80\u5355\u7684\u65b9\u6cd5\u6765\u5224\u65ad\u5f53\u524d\u7684\u6a21\u5757\u662f<strong>\u88ab\u76f4\u63a5\u8fd0\u884c<\/strong>\u8fd8\u662f<strong>\u88ab\u8c03\u7528<\/strong>\uff0c\u8fd9\u662f\u5f88\u91cd\u8981\u7684\u529f\u80fd\uff0c\u5982\uff1a<\/p>\n\n\n\n<ul><li>\u6211\u4eec\u53ef\u4ee5\u628a<strong>\u4e0d\u60f3\u5728\u88ab\u8c03\u7528\u65f6\u6267\u884c\u7684\u4ee3\u7801<\/strong>\u653e\u5728\u7a0b\u5e8f\u5165\u53e3\u7684if\u5185\u90e8\uff0c\u6bd4\u5982\u81ea\u68c0\u7a0b\u5e8f\u3002<\/li><li><em><strong>\u6211\u4eec\u8fd8\u628a\u591a\u7ebf\u7a0b\u7684\u4e3b\u7ebf\u7a0b\u5199\u5728\u7a0b\u5e8f\u5165\u53e3\u7684if\u5185\u90e8\u3002\u53ea\u80fd\u8fd9\u4e48\u505a\uff0c\u907f\u514d\u81ea\u5df1\u8c03\u7528\u81ea\u5df1\u65f6\u91cd\u590d\u6267\u884c\u4e3b\u8fdb\u7a0b\uff0c\u4e0b\u9762\u4f1a\u8be6\u7ec6\u89e3\u91ca\u3002<\/strong><\/em><\/li><\/ul>\n\n\n\n<p>\u56e0\u6b64\uff0c\u521d\u5b66Python\u65f6\uff0c\u76f4\u63a5\u628a\u4e3b\u7a0b\u5e8f\u5199\u5728\u4e0d\u9700\u8981\u7f29\u8fdb\u7684\u4f4d\u7f6e\uff0c\u5b8c\u5168\u4e0d\u5199 if __name__ ==&#8217;__main__&#8217;: \u5f53\u7136\u53ef\u4ee5\u3002\u4e00\u4e2a\u65e2\u6ca1\u6709\u5199\u4e00\u4e2a\u88ab\u8c03\u7528\u7684\u5e93\u7684\u80fd\u529b\uff0c\u4e5f\u4e0d\u4e00\u5b9a\u8981\u5b66\u591a\u8fdb\u7a0b\u7684\u65b0\u624b\uff0c\u5f88\u5bb9\u6613\u9519\u8bef\u5730\u8ba4\u4e3a\u300c\u7a0b\u5e8f\u7684\u5165\u53e3\u300d\u6ca1\u4ec0\u4e48\u7528\u3002<\/p>\n\n\n\n<p>\u7c7b\u4f3c\u7684\uff0c\u8fd8\u6709\u88ab\u5c11\u6570\u4eba\u8bef\u89e3\u7684\u8fd8\u6709 Python\u7684\u6587\u4ef6\u5934\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/bin\/bash\/python3  # \u8fd9\u4e00\u53e5\u8bdd\u7528\u6765\u5728\u4ee3\u7801\u88ab\u6267\u884c\u65f6\uff0c\u4e3b\u52a8\u8bf4\u660e\u8be5\u9009\u54ea\u4e2a\u8def\u5f84\u4e0b\u7684\u7f16\u8bd1\u5668\n<em>#!\/bin\/bash\/python2  # \u4f8b\u5982\u8fd9\u4e00\u53e5\u5c31\u9009\u4e86Python2\uff0c\u4e0d\u8fc72020\u5e74Python2\u5feb\u8981\u5b8c\u6210\u8fc7\u6e21\u4f7f\u547d\u4e86<\/em><\/code><\/pre>\n\n\n\n<p>2020\u5e74\u5e95\uff0c\u6211\u5728\u5199Python\u591a\u8fdb\u7a0b\u6559\u7a0b\u65f6\uff0c\u6ca1\u6709\u641c\u7d22\u5230\u5408\u9002\u7684\u6587\u7ae0\u89e3\u91ca\u201c\u7a0b\u5e8f\u7684\u5165\u53e3 if __name__ ==&#8217;__main__&#8217;: \u4e0e\u591a\u7ebf\u7a0b\u7684\u5fc5\u8981\u8054\u7cfb\u201d\uff0c\u53cd\u800c\u770b\u5230\u4e86\u5f88\u591a\u9ad8\u8d5e\u7684\u7247\u9762\u56de\u7b54\u3002\u65e0\u5948\u4e4b\u4e0b\u53ea\u80fd\u81ea\u5df1\u5199\u3002\u5bf9\u4e8e\u5c11\u6570\u6709\u57fa\u7840\u7684\u4eba\uff0c\u4e0b\u9762\u8bb2<strong>\u7a0b\u5e8f\u5165\u53e3\u4e0e\u591a\u7ebf\u7a0b<\/strong>\u90e8\u5206\u4e5f\u503c\u5f97\u4e00\u770b\u3002<\/p>\n\n\n\n<h2>\u76ee\u5f55<\/h2>\n\n\n\n<ul><li>\u300c\u7a0b\u5e8f\u7684\u5165\u53e3\u300d\u662f\u4ec0\u4e48\uff1f<\/li><li>\u7a0b\u5e8f\u5165\u53e3\u4e0e\u81ea\u68c0\u7a0b\u5e8f<\/li><li>\u7a0b\u5e8f\u5165\u53e3\u4e0e\u591a\u7ebf\u7a0b<\/li><\/ul>\n\n\n\n<p>\u66f4\u65b0\u65e5\u5fd7<\/p>\n\n\n\n<ul><li>\u7b2c\u4e00\u7248 2021-01-01 \u6211\u4f1a\u628a\u8bc4\u8bba\u533a\u7684\u597d\u95ee\u9898\u66f4\u65b0\u5230\u6b63\u6587\u91cc<\/li><\/ul>\n\n\n\n<h2>\u300c\u7a0b\u5e8f\u7684\u5165\u53e3\u300d\u662f\u4ec0\u4e48\uff1f<\/h2>\n\n\n\n<p>\u7528\u5f88\u77ed\u7684\u8bdd\u5c31\u80fd\u89e3\u91ca\uff0c\u6211\u8ba4\u53ef<a href=\"https:\/\/www.zhihu.com\/question\/49136398\/answer\/331861726\">\u83dc\u9e1f\u5206\u6790\u2193\u7684\u56de\u7b54<\/a>&nbsp;\uff0c\u90e8\u5206\u9ad8\u8d5e\u7b54\u6848\u5199\u5f97\u5570\u55e6<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>__name__ \u662f\u5f53\u524d\u6a21\u5757\u540d\uff0c\u5f53\u6a21\u5757\u88ab<strong>\u76f4\u63a5\u8fd0\u884c\u65f6\u6a21\u5757\u540d\u4e3a __main__&nbsp;<\/strong>\u3002\u8fd9\u53e5\u8bdd\u7684\u610f\u601d\u5c31\u662f\uff0c\u5f53\u6a21\u5757\u88ab\u76f4\u63a5\u8fd0\u884c\u65f6\uff0c\u4ee5\u4e0b\u4ee3\u7801\u5757\u5c06\u88ab\u8fd0\u884c\uff0c\u5f53\u6a21\u5757\u662f\u88ab\u5bfc\u5165\u65f6\uff0c\u4ee3\u7801\u5757\u4e0d\u88ab\u8fd0\u884c\u3002<\/p><\/blockquote>\n\n\n\n<p>\u4e3e\u4f8b\u8bf4\u660e\uff1a\u5f53\u6211\u5728\u7ec8\u7aef\u76f4\u63a5\u8fd0\u884c python3<strong>&nbsp;run1.py<\/strong>\u65f6\uff0c\u6a21\u5757\u540d\u88ab\u4e00\u5f8b\u6539\u4e3a\u5b57\u7b26\u4e32__main__\uff0c\u5f53\u6a21\u5757\u662f\u88ab\u53e6\u4e00\u4e2a *.py\u7a0b\u5e8f\u5bfc\u5165\uff08\u5982\u5728 *.py \u4e2d import run1\uff09\u800c\u4e0d\u662f\u76f4\u63a5\u8fd0\u884c\u65f6\uff0c\u6a21\u5757\u540d\u662f\u5b57\u7b26\u4e32<strong>run1<\/strong>\u3002<\/p>\n\n\n\n<p>\u5728C\u8bed\u8a00\u548cJava\u91cc\u4e5f\u6709\u7c7b\u4f3c\u7684\u300c\u7a0b\u5e8f\u5165\u53e3\u300d\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><em># Python\u7684\u7a0b\u5e8f\u5165\u53e3<\/em>\nif __name__ =='__main__': <em># \u5b83\u5bf9\u591a\u8fdb\u7a0b\u975e\u5e38\u91cd\u8981<\/em>\n    <em># \u8fd9\u91cc\u662f\u4e3b\u7a0b\u5e8f<\/em>\n\n<em># C\u8bed\u8a00\u7684\u7a0b\u5e8f\u5165\u53e3<\/em>\nvoid main(){\n    \/* \u8fd9\u91cc\u662f\u4e3b\u7a0b\u5e8f *\/\n}\n\n<em># Java\u7684\u7a0b\u5e8f\u5165\u53e3<\/em>\npublic static void main(String&#91;] args){\n    \/\/ \u8fd9\u91cc\u662f\u4e3b\u7a0b\u5e8f\n}<\/code><\/pre>\n\n\n\n<p>\u68c0\u9a8c\u4e00\u4e0b\u81ea\u5df1\uff1a\u4e0b\u9762\u7684\u7a0b\u5e8f\u4f1aprint\u51fa\u4ec0\u4e48\u4e1c\u897f\uff1f<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><em># \u65b0\u5efa\u4e00\u4e2a\u540d\u4e3a\u3010run1.py\u3011\u7684\u6587\u4ef6\uff0c\u586b\u5165\u4e0b\u65b9\u4ee3\u7801<\/em>\n<em># \u7136\u540e\u5728\u7ec8\u7aef\u8f93\u5165\u3010python3 run1.py\u3011\u5e76\u8fd0\u884c<\/em>\n\nprint(__name__, 'run1-outside')    <em># \u5b83\u4f1aprint\u51fa\u3010__main__ run1-outside\u3011<\/em>\nif __name__ =='__main__':\n    print(__name__, 'run1-inside') <em># \u5b83\u4f1aprint\u51fa\u3010__main__ run1-inside\u3011<\/em><\/code><\/pre>\n\n\n\n<p>\u518d\u68c0\u9a8c\u4e00\u4e0b\u81ea\u5df1\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><em># \u65b0\u5efa\u53e6\u4e00\u4e2a\u540d\u4e3a\u3010run2.py\u3011\u7684\u6587\u4ef6\uff0c\u586b\u5165\u4e0b\u65b9\u4ee3\u7801\uff0c\u5e76\u653e\u5728\u4e0e\u3010run1.py\u3011\u7684\u76f8\u540c\u76ee\u5f55\u4e0b\uff0c<\/em>\n<em># \u7136\u540e\u5728\u7ec8\u7aef\u8f93\u5165\u3010python3 run2.py\u3011\u5e76\u8fd0\u884c\u3002\u7528run2 \u8c03\u7528run1<\/em>\n\nimport run1  <em># \u8fd9\u4e00\u884c\u4ee3\u7801\u8c03\u7528\u4e86\u5916\u90e8\u7684\u4ee3\u7801 run1\uff0c\u5b83\u53ea\u4f1aprint\u51fa\uff1a<\/em>\n<em># \u3010run1 run1-outside\u3011 # \u5b83\u53eaprint\u51fa\u8fd9\u4e00\u884c\u4e1c\u897f\uff0c\u5e76\u4e14run1.py\u7684\u3010__main__\u3011\u53d8\u6210\u4e86\u3010run1\u3011<\/em>\n<em># \u3010run1 run1-inside\u3011  # \u5199\u5728run1\u3010if\u3011\u7f29\u8fdb\u91cc\u7684\u4e1c\u897f\u90fd\u6ca1\u6709\u88ab\u6267\u884c<\/em>\n\nprint(__name__, 'run2-outside')    <em># \u5b83\u4f1aprint\u51fa\u3010__main__ run2-outside\u3011<\/em>\nif __name__ =='__main__':\n    print(__name__, 'run2-inside') <em># \u5b83\u4f1aprint\u51fa\u3010__main__ run2-inside\u3011<\/em><\/code><\/pre>\n\n\n\n<h2>\u7a0b\u5e8f\u5165\u53e3\u4e0e\u81ea\u68c0\u7a0b\u5e8f<\/h2>\n\n\n\n<p>\u5f53\u4e00\u4e2a\u5f00\u53d1\u8005\u7f16\u5199\u4e00\u4e2a\u5e93\u65f6\uff08\u4f8b\u5982\u628a\u5b83\u547d\u540d\u4e3a utils.py\uff09\uff0c\u5982<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><em># \u8fd9\u4e2a\u5e93\uff08\u6a21\u5757\uff09\u88ab\u547d\u540d\u4e3a utils.py<\/em>\nclass C1:\n   ...\ndef func1():\n   ...\n\nc1 = C1()  <em># \u8fd9\u662f\u9519\u8bef\u7684\u505a\u6cd5\uff0c\u5e94\u8be5\u632a\u5230 \u7a0b\u5e8f\u5165\u53e3if\u5185\u90e8<\/em>\nfunc1()    <em># \u8fd9\u662f\u9519\u8bef\u7684\u505a\u6cd5\uff0c\u5e94\u8be5\u632a\u5230 \u7a0b\u5e8f\u5165\u53e3if\u5185\u90e8<\/em>\nif __name__ =='__main__':\n    c = C1()\n    func1()<\/code><\/pre>\n\n\n\n<p>\u5f53\u5176\u4ed6\u4eba\u53ea\u60f3\u8c03\u7528 C1 \u6216\u8005 func1\u65f6\uff0c\u4ed6\u5728\u53e6\u4e00\u4e2aPython\u6587\u4ef6\u4e2d\uff0c\u7528 import utils \u5bfc\u5165 utils\u8fd9\u4e2a\u5e93\u65f6\u5c31\u4e0d\u4f1a\u8fd0\u884c\u7a0b\u5e8f\u5165\u53e3if\u5185\u90e8\u7684\u4efb\u4f55\u4ee3\u7801\u4e86\u3002<\/p>\n\n\n\n<h2>\u7a0b\u5e8f\u5165\u53e3\u4e0e\u591a\u7ebf\u7a0b<\/h2>\n\n\n\n<p>\u5b9e\u73b0\u591a\u7ebf\u7a0b\u65f6\uff0c\u300c\u7a0b\u5e8f\u5165\u53e3\u300d\u8fd9\u4e2a\u529f\u80fd<strong>\u4e0d\u53ef\u6216\u7f3a<\/strong>\u3002\u6211\u9700\u8981\u540c\u65f6\u8fd0\u884c\u591a\u4e2a fun1\uff0c\u6216\u8005\u540c\u65f6\u8fd0\u884c fun1 fun2 &#8230; \u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def function1(id):  <em># \u8fd9\u91cc\u662f\u5b50\u8fdb\u7a0b<\/em>\n    print(f'id {id}')\n\ndef run__process():  <em># \u8fd9\u91cc\u662f\u4e3b\u8fdb\u7a0b<\/em>\n    from multiprocessing import Process\n    process = &#91;mp.Process(target=function1, args=(1,)),\n               mp.Process(target=function1, args=(2,)), ]\n    &#91;p.start() for p in process]  <em># \u5f00\u542f\u4e86\u4e24\u4e2a\u8fdb\u7a0b<\/em>\n    &#91;p.join() for p in process]   <em># \u7b49\u5f85\u4e24\u4e2a\u8fdb\u7a0b\u4f9d\u6b21\u7ed3\u675f<\/em>\n\n<em># run__mp()  # \u4e3b\u7ebf\u7a0b\u4e0d\u5efa\u8bae\u5199\u5728 if\u5916\u90e8\u3002\u7531\u4e8e\u8fd9\u91cc\u7684\u4f8b\u5b50\u5f88\u7b80\u5355\uff0c\u4f60\u5f3a\u884c\u8fd9\u4e48\u505a\u53ef\u80fd\u4e0d\u4f1a\u62a5\u9519<\/em>\nif __name__ =='__main__':\n    run__mp()  <em># \u6b63\u786e\u505a\u6cd5\uff1a\u4e3b\u7ebf\u7a0b\u53ea\u80fd\u5199\u5728 if\u5185\u90e8<\/em><\/code><\/pre>\n\n\n\n<p class=\"has-bright-blue-background-color has-background\"><strong>\u5f53\u6211\u8fd0\u884c\u4e0a\u9762\u8fd9\u4e2a\u7a0b\u5e8f\uff0c\u5b83\u7684\u3010__name__ ==&#8217;__main__&#8217;\u3011\uff0c\u56e0\u6b64\u5b83\u4f1a\u6267\u884c \u3010if\u3011\u5185\u7684\u4ee3\u7801\u3002\u8fd9\u4e9b\u4ee3\u7801\u4f1a\u521b\u5efa\u65b0\u7684\u591a\u4e2a\u5b50\u8fdb\u7a0b\uff0c\u81ea\u5df1\u8c03\u7528\u81ea\u5df1\u3002\u5728\u88ab\u8c03\u7528\u7684\u5b50\u8fdb\u7a0b\u4e2d\uff0c\u5b83\u7684\u3010__name__\u3011 \u4e0d\u7b49\u4e8e\u3010__main__\u3011\uff0c\u56e0\u6b64\u5b83\u53ea\u4f1a\u6267\u884c\u88ab\u4e3b\u8fdb\u7a0b\u5206\u914d\u7684\u4efb\u52a1\uff08\u6bd4\u5982fun1\uff09\uff0c\u800c\u4e0d\u4f1a\u50cf\u4e3b\u8fdb\u7a0b\u4e00\u6837\u901a\u8fc7\u300c\u7a0b\u5e8f\u5165\u53e3\u300d\u518d\u8c03\u7528\u522b\u7684\u8fdb\u7a0b\uff08\u884c\u6b64\u50ed\u8d8a\u4e4b\u4e8b\uff09\u3002\u8fd9\u662f\u4e00\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u529f\u80fd\uff0c\u8fd9\u91cc\u8bb2\u7684\u4e0d\u4ec5\u662fPython\uff0c\u5176\u4ed6\u6210\u719f\u7684\u7f16\u7a0b\u8bed\u8a00\u4e5f\u80fd\u7528\u76f8\u4f3c\u7684\u65b9\u6cd5\u3002<\/strong><\/p>\n\n\n\n<p><a href=\"https:\/\/github.com\/pytorch\/pytorch\/issues\/3492#issuecomment-341965218\" target=\"_blank\" rel=\"noreferrer noopener\">RuntimeError: context has already been set(multiprocessing) #3492 PyTorch Issue<\/a><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/pic4.zhimg.com\/v2-2b9cbc57af288b96bd8a84e9d4dc76eb_r.jpg\" alt=\"\"\/><figcaption>\u5c3d\u7ba1\u6709\u5f88\u591a\u4eba\u70b9\u8e29\uff0c\u4f46\u662f\u8fd9\u4e2a\u5206\u6790\u662f\u6b63\u786e\u7684\u3002\u70b9\u8e29\u7684\u53ef\u80fd\u662f\u5176\u4ed6\u539f\u56e0\u5f15\u53d1\u4e86\u9519\u8bef<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/pic3.zhimg.com\/v2-9afe1b9cbd9c677c7b8233a235b0760e_r.jpg\" alt=\"\"\/><figcaption>\u5c3d\u7ba1forkserver \u4f9d\u7136\u4e0d\u5982 spawn\u66f4\u8282\u7701\u8d44\u6e90\uff0c\u4f46\u80fd\u89e3\u51b3\u95ee\u9898\u4e5f\u7b97\u4e0d\u9519\u4e86<\/figcaption><\/figure>\n\n\n\n<p>\u7531\u4e8e\u6211\u4e0a\u9762\u7684\u4f8b\u5b50\u8fc7\u4e8e\u7b80\u5355\uff08\u6ca1\u6709\u6d89\u53ca\u8fdb\u7a0b\u901a\u4fe1\u3001\u8fdb\u7a0b\u9000\u51fa\u6761\u4ef6\uff09\uff0c<strong>\u5982\u679c\u4f60\u5f3a\u884c\u628a\u4e3b\u8fdb\u7a0b\u5199\u5728 if\u5916\u90e8\uff0c\u4e5f\u53ef\u80fd\u4e0d\u4f1a\u770b\u5230\u62a5\u9519\u3002<\/strong>\u8fd9\u6d89\u53ca\u5f88\u591a\u56e0\u7d20\uff0c\u5b83\u4e0e\u4f60\u4f7f\u7528\u7684\u7cfb\u7edf\u3001\u5b50\u8fdb\u7a0b\u7684\u521b\u5efa\u65b9\u5f0f\uff08spwan\u3001fork\u3001forkserver\u3001force=True\/False\uff09\u6709\u5173\u3002\u6211\u5728\u8fd9\u91cc\u53ea\u8bb2\u300c\u7a0b\u5e8f\u7684\u5165\u53e3\u300d\uff0c\u66f4\u591a\u5185\u5bb9\u8bf7\u79fb\u6b65&nbsp;<a href=\"https:\/\/stackoverflow.com\/a\/20361032\/9293137\" target=\"_blank\" rel=\"noreferrer noopener\">Compulsory usage of if __name__==\u201c__main__\u201d in windows while using multiprocessing &#8211; Stack Overflow<\/a>&nbsp;\uff0cTim Peters \u4e0e David Heffernan \u7684\u56de\u7b54\u90fd\u4e0d\u9519\u3002<\/p>\n\n\n\n<p>\u5c3d\u7ba1Python\u7684\u591a\u8fdb\u7a0b\u5df2\u7ecf\u505a\u5f97\u633a\u4e0d\u9519\u4e86\uff0c\u5e0c\u671b\u968f\u7740\u4ee5\u540e\u7248\u672c\u7684\u66f4\u65b0\uff0c\u591a\u8fdb\u7a0b\u4e0e\u300c\u7a0b\u5e8f\u5165\u53e3\u300d\u7684\u4f9d\u8d56\u5173\u7cfb\u5e94\u8be5\u80fd\u5f97\u5230\u66f4\u597d\u7684\u89e3\u51b3\u3002<\/p>\n\n\n\n<h2>\u4f7f\u7528PyTorch CUDA multiprocessing \u7684\u65f6\u5019\u51fa\u73b0\u7684\u9519\u8bef UserWarning: semaphore_tracker<\/h2>\n\n\n\n<p>\uff08\u5199\u4e8e2021-03-03\uff09<\/p>\n\n\n\n<p>\u9519\u8bef\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>multiprocessing\/semaphore_tracker.py:144: \nUserWarning: semaphore_tracker: There appear to be 1 leaked semaphores to clean up at shutdown\n  len(cache))\n\nIssue with multiprocessing semaphore tracking<\/code><\/pre>\n\n\n\n<p>\u76f8\u540c\u95ee\u9898\u63cf\u8ff0\uff1a<\/p>\n\n\n\n<p><a href=\"https:\/\/github.com\/keras-team\/autokeras\/issues\/200\" target=\"_blank\" rel=\"noreferrer noopener\">semaphore_tracker: There appear to be 1 leaked semaphores to clean up at shutdown len(cache)) #200<\/a><\/p>\n\n\n\n<p>\u89e3\u51b3\u65b9\u6848\uff1a<\/p>\n\n\n\n<p><a href=\"https:\/\/discuss.pytorch.org\/t\/issue-with-multiprocessing-semaphore-tracking\/22943\/4\" target=\"_blank\" rel=\"noreferrer noopener\">Issue with multiprocessing semaphore tracking &#8211; sbelharbi \u7684\u89e3\u51b3\u65b9\u6848<\/a><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/pic2.zhimg.com\/v2-69aa8dc2c218d40ded2ed2ee2b32a5d5_r.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<p>\u5373\u5728\u8fd0\u884c .py \u6587\u4ef6\u524d\uff0c\u4f7f\u7528\u4ee5\u4e0b\u8bed\u53e5\u4fee\u6539\u73af\u5883\u53c2\u6570\uff0c\u5ffd\u7565\u8fd9\u4e2aWarning \u5e26\u6765\u7684\u7a0b\u5e8f\u6682\u505c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>export PYTHONWARNINGS='ignore:semaphore_tracker:UserWarning'<\/code><\/pre>\n\n\n\n<p>\u7b49\u540c\u4e8e\u5728 .py \u6587\u4ef6\u5185\u90e8\u4f7f\u7528\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>os.environ&#91;'PYTHONWARNINGS'] = 'ignore:semaphore_tracker:UserWarning'<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6458\u81ea\uff1ahttps:\/\/zhuanlan.zhihu.com\/p\/340965963 \u5728Python\u4e2d\uff0c\u88ab\u79f0\u4e3a\u300c &hellip; <a href=\"http:\/\/139.9.1.231\/index.php\/2023\/03\/06\/python_name___main\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">Python\u7a0b\u5e8f\u5165\u53e3 __name__ == &#8216;__main__&#8217; \u6709\u91cd\u8981\u529f\u80fd\uff08\u591a\u7ebf\u7a0b\uff09\u800c\u975e\u7f16\u7a0b\u4e60\u60ef<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[8],"tags":[],"_links":{"self":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/14681"}],"collection":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/comments?post=14681"}],"version-history":[{"count":6,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/14681\/revisions"}],"predecessor-version":[{"id":14800,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/14681\/revisions\/14800"}],"wp:attachment":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/media?parent=14681"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/categories?post=14681"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/tags?post=14681"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}