{"id":14679,"date":"2023-03-06T22:19:27","date_gmt":"2023-03-06T14:19:27","guid":{"rendered":"http:\/\/139.9.1.231\/?p=14679"},"modified":"2023-03-06T22:22:47","modified_gmt":"2023-03-06T14:22:47","slug":"python_multiprosess","status":"publish","type":"post","link":"http:\/\/139.9.1.231\/index.php\/2023\/03\/06\/python_multiprosess\/","title":{"rendered":"\u5728Python\u4e2d\u4f18\u96c5\u5730\u7528\u591a\u8fdb\u7a0b"},"content":{"rendered":"\n<figure class=\"wp-block-image\"><img src=\"https:\/\/pica.zhimg.com\/v2-adbf9a1e49b6ba773cac367ed00a7907_720w.jpg?source=172ae18b\" alt=\"\u5728Python\u4e2d\u4f18\u96c5\u5730\u7528\u591a\u8fdb\u7a0b\"\/><\/figure>\n\n\n\n<p class=\"has-text-align-center has-light-pink-background-color has-background\"><strong>\u6458\u81ea\u77e5\u4e4e\uff1a<a href=\"https:\/\/zhuanlan.zhihu.com\/p\/340657122\">https:\/\/zhuanlan.zhihu.com\/p\/340657122<\/a><\/strong><\/p>\n\n\n\n<p><a href=\"https:\/\/docs.python.org\/3.9\/library\/multiprocessing.html\" target=\"_blank\" rel=\"noreferrer noopener\">Python\u81ea\u5e26\u7684\u591a\u8fdb\u7a0b\u5e93 multiprocessing<\/a>&nbsp;\u53ef\u5b9e\u73b0\u591a\u8fdb\u7a0b\u3002\u6211\u60f3\u7528\u8fd9\u4e9b\u77ed\u4f8b\u5b50\u793a\u8303\u5982\u4f55\u4f18\u96c5\u5730\u7528\u591a\u7ebf\u7a0b\u3002\u4e2d\u6587\u7f51\u7edc\u4e0a\uff0c\u6709\u4e9b\u4eba\u53ea\u662f\u7ffb\u8bd1\u4e86\u65e7\u7248\u7684 Python\u5b98\u7f51\u7684\u591a\u8fdb\u7a0b\u6587\u6863\u3002\u800c\u6211\u8fd9\u7bc7\u6587\u7ae0\u4f1a\u989d\u5916\u8bb2\u4e00\u8bb2\u4e0b\u65b9\u52a0\u7c97\u90e8\u5206\u7684\u5185\u5bb9\u3002<\/p>\n\n\n\n<ul><li>\u521b\u5efa\u8fdb\u7a0b Process\uff0c<strong>fork\u76f4\u63a5\u7ee7\u627f\u8d44\u6e90\uff0c\u6240\u4ee5\u521d\u59cb\u5316\u66f4\u5feb\uff0cspawn\u53ea\u7ee7\u627f\u5fc5\u8981\u7684\u8d44\u6e90\uff0c\u6240\u4ee5\u66f4\u7701\u5185\u5b58\uff0c\u300c\u7a0b\u5e8f\u7684\u5165\u53e3\u300d if name == main<\/strong><\/li><li>\u8fdb\u7a0b\u6c60 Pool\uff0c<strong>Pool\u53ea\u80fd\u63a5\u53d7\u4e00\u4e2a\u53c2\u6570\uff0c\u4f46\u6709\u529e\u6cd5\u4f20\u5165\u591a\u4e2a<\/strong><\/li><li>\u7ba1\u9053\u901a\u4fe1 Pipe\uff0c<strong>\u6700\u57fa\u672c\u7684\u529f\u80fd\uff0c\u8fd0\u884c\u901f\u5ea6\u5feb<\/strong><\/li><li>\u961f\u5217\u901a\u4fe1 Queue\uff0c<strong>\u6709\u6700\u5e38\u7528\u7684\u529f\u80fd\uff0c\u8fd0\u884c\u901f\u5ea6\u7a0d\u6162<\/strong><\/li><li>\u5171\u4eab\u5185\u5b58 Manager Value\uff0c<strong>Python3.9 \u65b0\u7279\u6027&nbsp;<\/strong><a href=\"https:\/\/docs.python.org\/3\/library\/multiprocessing.shared_memory.html\" target=\"_blank\" rel=\"noreferrer noopener\">\u771f\u6b63\u7684\u5171\u4eab\u5185\u5b58 shared_memory<\/a><\/li><\/ul>\n\n\n\n<p>\u5982\u4e0b\u6240\u793a\uff0c\u4e2d\u6587\u7f51\u7edc\u4e0a\u4e00\u4e9b\u8bb2Python\u591a\u8fdb\u7a0b\u7684\u6587\u7ae0\uff0c\u5f88\u591a\u91cd\u8981\u7684\u4e1c\u897f\u6ca1\u8bb2\uff08\u6bd5\u7adf\u53ea\u662f\u7ffb\u8bd1\u4e86Python\u5b98\u7f51\u7684\u591a\u8fdb\u7a0b\u65e7\u7248\u6587\u6863\uff09\u3002\u4e0a\u65b9\u7684\u52a0\u7c97\u90e8\u5206\u4ed6\u4eec\u6ca1\u8bb2\uff0c\u4f46\u662f\u8fd9\u662f\u505a\u591a\u8fdb\u7a0b\u603b\u9700\u8981\u77e5\u9053\u7684\u5185\u5bb9\u3002<\/p>\n\n\n\n<ul><li>\u82e5\u4f60\u65e0\u6cd5\u6d41\u7545\u9605\u8bfb\u6709\u4e13\u4eba\u66f4\u65b0\u7684<a href=\"https:\/\/docs.python.org\/3.9\/library\/multiprocessing.html\" target=\"_blank\" rel=\"noreferrer noopener\">Python\u5b98\u7f51\u591a\u8fdb\u7a0b\u82f1\u6587\u6587\u6863<\/a>&nbsp;\uff0c\u90a3\u4e48\u59d1\u4e14\u53ef\u770b\u5199\u4e8e2019\u4e0d\u4fdd\u8bc1\u66f4\u65b0\u7684<a href=\"https:\/\/zhuanlan.zhihu.com\/p\/64702600\">\u5357\u5c71\u5357\uff1a\u4e00\u7bc7\u6587\u7ae0\u641e\u5b9aPython\u591a\u8fdb\u7a0b(\u5168)<\/a><\/li><li><a href=\"https:\/\/zhuanlan.zhihu.com\/p\/20953544\">\u77e5\u6211\u83ab\u8a00\uff1a\u8c08\u8c08python\u7684GIL\u3001\u591a\u7ebf\u7a0b\u3001\u591a\u8fdb\u7a0b<\/a>&nbsp;\uff0c\u70b9\u8d5e\u591a\u4f46\u65e7\uff0c\u5199\u4e8e2016\uff0c\u4f60\u8fd8\u4e0d\u5982\u770b\u6211\u4e0b\u65b9\u5199\u7684\u300c\u7b80\u8ff0\u4f55\u4e3a\u591a\u7ebf\u7a0b threading\u4e0e\u591a\u8fdb\u7a0b processing\u300d<\/li><\/ul>\n\n\n\n<h2>\u76ee\u5f55\uff08\u8bf7\u6311\u9009\u611f\u5174\u8da3\u7684\u770b\uff0c\u6ca1\u5fc5\u8981\u5168\u770b\uff09<\/h2>\n\n\n\n<ol><li>\u591a\u7ebf\u7a0b\u4e0e\u591a\u8fdb\u7a0b\u7684\u533a\u522b<\/li><li>\u5168\u5c40\u9501\u4e0e\u591a\u8fdb\u7a0b<\/li><li>\u5b50\u8fdb\u7a0b Process<\/li><li>\u8fdb\u7a0b\u6c60 Pool<\/li><li>\u7ba1\u9053 Pipe<\/li><li>\u961f\u5217 Queue<\/li><li>\u5171\u4eab\u5185\u5b58 Manager<\/li><li>\u56de\u7b54\u8bc4\u8bba\u533a\u7684\u6709\u7528\u95ee\u9898\uff08\u522b\u79c1\u4fe1\uff09<\/li><li>\u6211\u4e3a\u4f55\u5199\u3010\u5728Python\u4e2d\u4f18\u96c5\u5730\u7528\u591a\u8fdb\u7a0b\u3011\uff1f<\/li><\/ol>\n\n\n\n<p>\u66f4\u65b0\u8bb0\u5f55\uff1a\u7b2c\u4e00\u7248 2021-1-4\uff0c\u7b2c\u4e8c\u7248 2021-1-8 \u88ab\u8feb\u66f4\u65b0\u4e86\u4e00\u4e9b\u79c1\u4fe1\u95ee\u5230\u7684\u95ee\u9898<\/p>\n\n\n\n<h2>1. \u591a\u7ebf\u7a0b\u4e0e\u591a\u8fdb\u7a0b\u7684\u533a\u522b<\/h2>\n\n\n\n<p><strong>\u591a\u7ebf\u7a0b threading\uff1a<\/strong>&nbsp;\u4e00\u4e2a\u4eba\u6709\u4e0e\u5f02\u6027\u804a\u5929\u548c\u770b\u5267\u4e24\u4ef6\u4e8b\u8981\u505a\u3002\u5355\u7ebf\u7a0b\u7684\u5979\u53ef\u4ee5\u770b\u5b8c\u5267\u518d\u53bb\u804a\u5929\uff0c\u4f46\u8fd9\u6837\u5b50\u53ef\u80fd\u5c31\u6ca1\u4eba\u966a\u5979\u804a\u5929\u4e86\u300c\u54fc\uff0c\u53d1\u6d88\u606f\u4e0d\u56de\u300d\u3002\u6211\u4eec\u628a\u5979\u770b\u6210\u4e00\u4e2aCPU\u6838\u5fc3\uff0c\u4e3a\u5979\u5f00\u8d77\u591a\u7ebf\u7a0b\u2014\u2014\u5148\u770b\u4e00\u4f1a\u5267\uff0c\u5076\u5c14\u770b\u770b\u65b0\u6d88\u606f\uff0c\u5728\u4e24\u4ef6\u4e8b\uff08\u7ebf\u7a0b\uff09\u95f4\u6765\u56de\u5207\u6362\u3002\u591a\u7ebf\u7a0b\uff1a\u5355\u4e2aCPU\u6838\u5fc3\u53ef\u4ee5\u540c\u65f6\u505a\u51e0\u4ef6\u4e8b\uff0c\u4e0d\u81f3\u4e8e\u5361\u5728\u67d0\u4e00\u6b65\u50bb\u7b49\u7740\u3002<\/p>\n\n\n\n<p>\u7528\u5904\uff1a\u722c\u53d6\u7f51\u7ad9\u4fe1\u606f\uff08\u722c\u866b\uff09\uff0c\u7b49\u5f85\u591a\u4e2a\u7528\u6237\u8f93\u5165<\/p>\n\n\n\n<p><strong>\u591a\u8fdb\u7a0b processing\uff1a<\/strong>&nbsp;\u4e00\u4e2a\u4eba\u6709\u5f88\u591a\u7816\u9700\u8981\u642c\uff0c\u4ed6\u9886\u53d6\u624b\u5957\u3001\u63a8\u8f66\u5404\u79cd\u7269\u8d44\uff08\u5411\u7cfb\u7edf\u7533\u8bf7\u4e86\u8d44\u6e90\uff09\u7136\u540e\u5f00\u59cb\u642c\u7816\u3002\u7136\u800c\u4ed6\u8eab\u8fb9\u6709\u5f88\u591a\u4eba\uff0c\u6211\u4eec\u8ba9\u8fd9\u4e9b\u4eba\u53bb\u5e2e\u4ed6\uff01\uff08\u4e00\u6838\u6709\u96be\uff0c\u516b\u6838\u56f4\u89c2\uff09\u3002\u4e8e\u662f\u4ed6\u4eec\u505a\u4e86\u5206\u5de5\uff0c\u7816\u5f88\u5feb\u5c31\u642c\u5b8c\u4e86\u3002\u591a\u8fdb\u7a0b\u8ba9\u591a\u4e2aCPU\u6838\u5fc3\u53ef\u4ee5\u4e00\u8d77\u505a\u4e8b\uff0c\u4e0d\u81f3\u4e8e\u53ea\u6709\u4e00\u4eba\u5e72\u6d3b\u800c\u5176\u4ed6\u4eba\u50bb\u7ad9\u7740\u3002<\/p>\n\n\n\n<p>\u7528\u5904\uff1a\u8fdb\u884c\u9ad8\u6027\u80fd\u8ba1\u7b97\u3002\u53ea\u6709\u591a\u8fdb\u7a0b\u65b9\u6848\u8bbe\u8ba1\u5408\u7406\uff0c\u624d\u80fd\u52a0\u901f\u8ba1\u7b97\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img src=\"https:\/\/pic4.zhimg.com\/80\/v2-025bd1af69fb7097e95c5fd65df933c3_1440w.webp\" alt=\"\"\/><figcaption>\u4e00\u6838\u6709\u96be\uff0c\u4e03\u6838\u56f4\u89c2<\/figcaption><\/figure><\/div>\n\n\n\n<h2>2. \u5168\u5c40\u9501\u4e0e\u591a\u8fdb\u7a0b<\/h2>\n\n\n\n<p><strong>\u4e3a\u4f55\u5728Python\u91cc\u7528\u591a\u8fdb\u7a0b\u8fd9\u4e48\u9ebb\u70e6\uff1f&nbsp;<\/strong>\u56e0\u4e3aPython\u7684\u7ebf\u7a0b\u662f\u64cd\u4f5c\u7cfb\u7edf\u7ebf\u7a0b\uff0c\u56e0\u6b64\u8981\u6709Python\u5168\u5c40\u89e3\u91ca\u5668\u9501\u3002\u4e00\u4e2apython\u89e3\u91ca\u5668\u8fdb\u7a0b\u5185\u6709\u4e00\u6761<strong>\u4e3b\u7ebf\u7a0b<\/strong>\uff0c\u4ee5\u53ca\u591a\u6761\u7528\u6237\u7a0b\u5e8f\u7684<strong>\u6267\u884c\u7ebf\u7a0b<\/strong>\u3002\u5373\u4f7f\u5728\u591a\u6838CPU\u5e73\u53f0\u4e0a\uff0c\u7531\u4e8eGIL\u7684\u5b58\u5728\uff0c\u6240\u4ee5\u7981\u6b62\u591a\u7ebf\u7a0b\u7684\u5e76\u884c\u6267\u884c\u3002\u2014\u2014\u6765\u81ea<a href=\"https:\/\/baike.baidu.com\/item\/%E5%85%A8%E5%B1%80%E8%A7%A3%E9%87%8A%E5%99%A8%E9%94%81\" target=\"_blank\" rel=\"noreferrer noopener\">\u767e\u5ea6\u767e\u79d1\u8bcd\u6761 \u5168\u5c40\u89e3\u91ca\u5668\u9501<\/a>\u3002\u53d1\u5c55\u5386\u7a0b\uff1a<\/p>\n\n\n\n<ol><li>Python\u5168\u5c40\u9501\u3002Python 3.2\u7684\u65f6\u5019\u66f4\u65b0\u8fc7GIL\u3002\u5728\u6211\u5c0f\u65f6\u5019\uff0c\u7531\u4e8e<a href=\"https:\/\/zhuanlan.zhihu.com\/p\/75780308\">Python GIL\u7684\u5b58\u5728\uff08\u5168\u5c40\u89e3\u91ca\u5668\u9501 Global Interpreter Lock\uff09<\/a>&nbsp;\uff0c\u6b64\u65f6Python\u65e0\u6cd5\u9760\u81ea\u5df1\u5b9e\u73b0\u591a\u8fdb\u7a0b<\/li><li>\u5916\u90e8\u591a\u8fdb\u7a0b\u901a\u4fe1\u3002Python3.5\u3002\u57282015\u5e74\uff0c\u8981\u4e48\u7528Python\u8c03\u7528C\u8bed\u8a00\uff08\u5982Numpy\u6b64\u7c7b\u7528\u5176\u4ed6\u8bed\u8a00\u5728\u5e95\u5c42\u5b9e\u73b0\u591a\u8fdb\u7a0b\u7684\u7b2c\u4e09\u65b9\u5e93\uff09\uff0c\u8981\u4e48\u9700\u8981\u5728\u5916\u90e8\u4ee3\u7801\uff08MPI 2015\uff09<\/li><li>\u5185\u7f6e\u591a\u8fdb\u7a0b\u901a\u4fe1\u3002Python 3.6 \u624d\u8ba9 multiprocessing\u9010\u6e10\u53d1\u5c55\u6210\u4e00\u4e2a\u80fd\u7528\u7684Python\u5185\u7f6e\u591a\u8fdb\u7a0b\u5e93\uff0c\u53ef\u4ee5\u8fdb\u884c\u8fdb\u7a0b\u95f4\u7684\u901a\u4fe1\uff0c\u4ee5\u53ca\u6709\u9650\u7684\u5185\u5b58\u5171\u4eab<\/li><li>\u5171\u4eab\u5185\u5b58\u3002Python 3.8 \u57282019\u5e74\u589e\u52a0\u4e86\u65b0\u7279\u6027 shared_memory<\/li><\/ol>\n\n\n\n<h2>3. \u5b50\u8fdb\u7a0b Process<\/h2>\n\n\n\n<p><strong>\u591a\u8fdb\u7a0b\u7684\u4e3b\u8fdb\u7a0b\u4e00\u5b9a\u8981\u5199\u5728\u7a0b\u5e8f\u5165\u53e3 if __name__ ==&#8217;__main__&#8217;: \u5185\u90e8<\/strong><\/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__process()  # \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__process()  <em># \u6b63\u786e\u505a\u6cd5\uff1a\u4e3b\u7ebf\u7a0b\u53ea\u80fd\u5199\u5728 if\u5185\u90e8<\/em><\/code><\/pre>\n\n\n\n<p>\u5c3d\u7ba1\u5728\u8fd9\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\u91cc\uff0c\u628a\u4e3b\u8fdb\u7a0brun__process()\u5199\u5728\u7a0b\u5e8f\u5165\u53e3if\u5916\u90e8\u4e0d\u4f1a\u6709\u62a5\u9519\u3002\u4f46\u662f\u4f60\u6700\u597d\u8fd8\u662f\u6309\u6211\u8981\u6c42\u53bb\u505a\u3002\u8be6\u7ec6\u89e3\u91ca\u7684\u5185\u5bb9\u8fc7\u957f\uff0c\u6211\u5199\u5728\u2192<a href=\"https:\/\/zhuanlan.zhihu.com\/p\/340965963\">\u300cPython\u7a0b\u5e8f\u5165\u53e3\u6709\u91cd\u8981\u529f\u80fd\uff08\u591a\u7ebf\u7a0b\uff09\u800c\u975e\u7f16\u7a0b\u4e60\u60ef\u300d<\/a><\/p>\n\n\n\n<p>\u4e0a\u9762\u7684\u4f8b\u5b50\u53ea\u662f\u7528Process\u5f00\u542f\u4e86\u591a\u8fdb\u7a0b\uff0c\u4e0d\u6d89\u53ca\u8fdb\u7a0b\u901a\u4fe1\u3002\u5f53\u6211\u51c6\u5907\u628a\u4e00\u4e2a\u4e32\u884c\u4efb\u52a1\u7f16\u6392\u6210\u591a\u8fdb\u7a0b\u65f6\uff0c\u6211\u8fd8\u9700\u8981\u591a\u8fdb\u7a0b\u901a\u4fe1\u3002\u8fdb\u7a0b\u6c60Pool\u53ef\u4ee5\u8ba9\u4e3b\u7a0b\u5e8f\u83b7\u5f97\u5b50\u8fdb\u7a0b\u7684\u8ba1\u7b97\u7ed3\u679c\uff08\u4e0d\u592a\u7075\u6d3b\uff0c\u9002\u5408\u7b80\u5355\u4efb\u52a1\uff09\uff0c\u7ba1\u9053Pipe \u961f\u5217Queue \u7b49\u7b49 \u53ef\u4ee5\u8ba9\u8fdb\u7a0b\u4e4b\u95f4\u8fdb\u884c\u901a\u4fe1\uff08\u8db3\u591f\u7075\u6d3b\uff09\u3002\u5171\u4eab\u503c Value \u5171\u4eab\u6570\u7ec4 Array \u5171\u4eab\u5185\u5bb9 shared_memory\uff08Python 3.6 Python3.9 \u7684\u65b0\u7279\u6027\uff0c\u8fd8\u4e0d\u592a\u6210\u719f\uff09\u4e0b\u9762\u5f00\u8bb2\u3002<\/p>\n\n\n\n<p>Python\u591a\u8fdb\u7a0b\u53ef\u4ee5\u9009\u62e9\u4e24\u79cd\u521b\u5efa\u8fdb\u7a0b\u7684\u65b9\u5f0f\uff0cspawn \u4e0e fork\u3002<strong>\u5206\u652f\u521b\u5efa\uff1afork<\/strong>\u4f1a\u76f4\u63a5\u590d\u5236\u4e00\u4efd\u81ea\u5df1\u7ed9\u5b50\u8fdb\u7a0b\u8fd0\u884c\uff0c\u5e76\u628a\u81ea\u5df1\u6240\u6709\u8d44\u6e90\u7684handle \u90fd\u8ba9\u5b50\u8fdb\u7a0b\u7ee7\u627f\uff0c\u56e0\u800c\u521b\u5efa\u901f\u5ea6\u5f88\u5feb\uff0c\u4f46\u66f4\u5360\u7528\u5185\u5b58\u8d44\u6e90\u3002<strong>\u5206\u4ea7\u521b\u5efa\uff1aspawn<\/strong>\u53ea\u4f1a\u628a\u5fc5\u8981\u7684\u8d44\u6e90\u7684handle \u4ea4\u7ed9\u5b50\u8fdb\u7a0b\uff0c\u56e0\u6b64\u521b\u5efa\u901f\u5ea6\u7a0d\u6162\u3002\u8be6\u7ec6\u89e3\u91ca\u8bf7\u770b&nbsp;<a href=\"https:\/\/stackoverflow.com\/questions\/64095876\/multiprocessing-fork-vs-spawn\" target=\"_blank\" rel=\"noreferrer noopener\">Stack OverFlow multiprocessing fork vs spawn<\/a>&nbsp;\u3002\uff08\u5206\u4ea7spawn \u662f\u6211\u81ea\u5df1\u968f\u4fbf\u7ffb\u8bd1\u7684\uff0c\u6709\u66f4\u597d\u7684\u7ffb\u8bd1\u8bf7\u63a8\u8350\u3002\u6211\u7edd\u4e0d\u628ahandle \u7ffb\u8bd1\u6210\u53e5\u67c4\uff09<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>multiprocessing.set_start_method('spawn')  # default on WinOS or MacOS\nmultiprocessing.set_start_method('fork')   # default on Linux (UnixOS)<\/code><\/pre>\n\n\n\n<p>\u8bf7\u6ce8\u610f\uff1a\u6211\u8bf4 \u5206\u652ffork \u5728\u521d\u59cb\u5316<strong>\u521b\u5efa\u591a\u8fdb\u7a0b<\/strong>\u7684\u65f6\u5019\u6bd4 \u5206\u4ea7spawn \u5feb\uff0c\u800c\u4e0d\u662f\u8bf4\u9ad8\u6027\u80fd\u8ba1\u7b97\u4f1a\u6bd4\u8f83\u5feb\u3002\u901a\u5e38\u9ad8\u6027\u80fd\u8ba1\u7b97\u9700\u8981\u8ba9\u7a0b\u5e8f\u8fd0\u884c\u5f88\u4e45\uff0c\u56e0\u6b64\u4e3a\u4e86\u8282\u7701\u5185\u5b58\u4ee5\u53ca\u8fdb\u7a0b\u5b89\u5168\uff0c\u6211\u5efa\u8bae\u9009\u62e9 spawn\u3002<\/p>\n\n\n\n<h2>4. \u8fdb\u7a0b\u6c60 Pool<\/h2>\n\n\n\n<p>\u51e0\u4e4ePython\u591a\u8fdb\u7a0b\u4ee3\u7801\u90fd\u9700\u8981\u4f60\u660e\u660e\u767d\u767d\u5730\u8c03\u7528Process\u3002\u800c\u8fdb\u7a0b\u6c60Pool \u4f1a\u81ea\u52a8\u5e2e\u6211\u4eec\u7ba1\u7406\u5b50\u8fdb\u7a0b\u3002Python\u7684Pool \u4e0d\u65b9\u4fbf\u4f20\u5165\u591a\u4e2a\u53c2\u6570\uff0c\u6211\u8fd9\u91cc\u63d0\u4f9b\u4e24\u4e2a\u89e3\u51b3\u601d\u8def\uff1a<\/p>\n\n\n\n<p>\u601d\u8def1\uff1a\u51fd\u6570 func2 \u9700\u8981\u4f20\u5165\u591a\u4e2a\u53c2\u6570\uff0c\u73b0\u5728\u628a\u5b83\u6539\u6210\u4e00\u4e2a\u53c2\u6570\uff0c\u65e0\u8bba\u4f60\u76f4\u63a5\u8ba9args\u4f5c\u4e3a\u4e00\u4e2a\u5143\u7ec4tuple\u3001\u8bcd\u5178dict\u3001\u7c7bclass\u90fd\u53ef\u4ee5<\/p>\n\n\n\n<p>\u601d\u8def2\uff1a\u4f7f\u7528 function.partial&nbsp;<a href=\"https:\/\/stackoverflow.com\/a\/25553970\/9293137\" target=\"_blank\" rel=\"noreferrer noopener\">Passing multiple parameters to pool.map() function in Python<\/a>\u3002\u8fd9\u4e2a\u4e0d\u7075\u6d3b\u7684\u65b9\u6cd5\u56fa\u5b9a\u4e86\u5176\u4ed6\u53c2\u6570\uff0c\u4e14\u9700\u8981\u5bfc\u5165Python\u7684\u5185\u7f6e\u5e93\uff0c\u6211\u4e0d\u63a8\u8350<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import time\n\ndef func2(args):  <em># multiple parameters (arguments)<\/em>\n    <em># x, y = args<\/em>\n    x = args&#91;0]  <em># write in this way, easier to locate errors<\/em>\n    y = args&#91;1]  <em># write in this way, easier to locate errors<\/em>\n\n    time.sleep(1)  <em># pretend it is a time-consuming operation<\/em>\n    return x - y\n\n\ndef run__pool():  <em># main process<\/em>\n    from multiprocessing import Pool\n\n    cpu_worker_num = 3\n    process_args = &#91;(1, 1), (9, 9), (4, 4), (3, 3), ]\n\n    print(f'| inputs:  {process_args}')\n    start_time = time.time()\n    with Pool(cpu_worker_num) as p:\n        outputs = p.map(func2, process_args)\n    print(f'| outputs: {outputs}    TimeUsed: {time.time() - start_time:.1f}    \\n')\n\n    '''Another way (I don't recommend)\n    Using 'functions.partial'. See https:\/\/stackoverflow.com\/a\/25553970\/9293137\n    from functools import partial\n    # from functools import partial\n    # pool.map(partial(f, a, b), iterable)\n    '''\n\nif __name__ =='__main__':\n    run__pool()<\/code><\/pre>\n\n\n\n<h2>5. \u7ba1\u9053 Pipe<\/h2>\n\n\n\n<p>\u987e\u540d\u601d\u4e49\uff0c\u7ba1\u9053Pipe \u6709\u4e24\u7aef\uff0c\u56e0\u800c main_conn, child_conn = Pipe() \uff0c\u7ba1\u9053\u7684\u4e24\u7aef\u53ef\u4ee5\u653e\u5728\u4e3b\u8fdb\u7a0b\u6216\u5b50\u8fdb\u7a0b\u5185\uff0c\u6211\u5728\u5b9e\u9a8c\u4e2d\u6ca1\u53d1\u73b0\u4e3b\u7ba1\u9053\u53e3main_conn \u548c\u5b50\u7ba1\u9053\u53e3child_conn \u7684\u533a\u522b\u3002\u4e24\u7aef\u53ef\u4ee5\u540c\u65f6\u653e\u8fdb\u53bb\u4e1c\u897f\uff0c\u653e\u8fdb\u53bb\u7684\u5bf9\u8c61\u90fd\u7ecf\u8fc7\u4e86\u6df1\u62f7\u8d1d\uff1a\u7528 conn.send()\u5728\u4e00\u7aef\u653e\u5165\uff0c\u7528 conn.recv() \u53e6\u4e00\u7aef\u53d6\u51fa\uff0c\u7ba1\u9053\u7684\u4e24\u7aef\u53ef\u4ee5\u540c\u65f6\u7ed9\u591a\u4e2a\u8fdb\u7a0b\u3002conn\u662f connect\u7684\u7f29\u5199\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import time\n\ndef func_pipe1(conn, p_id):\n    print(p_id)\n\n    time.sleep(0.1)\n    conn.send(f'{p_id}_send1')\n    print(p_id, 'send1')\n\n    time.sleep(0.1)\n    conn.send(f'{p_id}_send2')\n    print(p_id, 'send2')\n\n    time.sleep(0.1)\n    rec = conn.recv()\n    print(p_id, 'recv', rec)\n\n    time.sleep(0.1)\n    rec = conn.recv()\n    print(p_id, 'recv', rec)\n\n\ndef func_pipe2(conn, p_id):\n    print(p_id)\n\n    time.sleep(0.1)\n    conn.send(p_id)\n    print(p_id, 'send')\n    time.sleep(0.1)\n    rec = conn.recv()\n    print(p_id, 'recv', rec)\n\n\ndef run__pipe():\n    from multiprocessing import Process, Pipe\n\n    conn1, conn2 = Pipe()\n\n    process = &#91;Process(target=func_pipe1, args=(conn1, 'I1')),\n               Process(target=func_pipe2, args=(conn2, 'I2')),\n               Process(target=func_pipe2, args=(conn2, 'I3')), ]\n\n    &#91;p.start() for p in process]\n    print('| Main', 'send')\n    conn1.send(None)\n    print('| Main', conn2.recv())\n    &#91;p.join() for p in process]\n\nif __name__ =='__main__':\n    run__pipe()<\/code><\/pre>\n\n\n\n<p>\u5982\u679c\u8ffd\u6c42\u8fd0\u884c\u66f4\u5feb\uff0c<strong>\u90a3\u4e48\u6700\u597d\u4f7f\u7528\u7ba1\u9053Pipe\u800c\u975e\u4e0b\u9762\u4ecb\u7ecd\u7684\u961f\u5217Queue<\/strong>\uff0c\u8be6\u7ec6\u8bf7\u79fb\u6b65<a href=\"https:\/\/www.raspberrypi.org\/forums\/viewtopic.php?t=141576\" target=\"_blank\" rel=\"noreferrer noopener\">Python pipes and queues performance<\/a>&nbsp;\u2193<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>So yes,&nbsp;<strong>pipes are faster than queues<\/strong>&nbsp;&#8211; but only by 1.5 to 2 times, what did surprise me was that Python 3 is MUCH slower than Python 2 &#8211; most other tests I have done have been a bit up and down (as long as it is Python 3.4 &#8211; Python 3.2 seems to be a bit of a dog &#8211; especially for memory usage).<\/p><\/blockquote>\n\n\n\n<p>\u6211\u5c0f\u65f6\u5019\u66fe\u7ecf\u7528\u5230Python\u591a\u7ebf\u7a0b\u961f\u5217\u529f\u80fd\u5199\u8fc7\u4e00\u4e2a\u5b9e\u9645\u4f8b\u5b50 \u2193\uff0c\u82e5\u8ffd\u6c42\u6781\u81f4\u6027\u80fd\uff0c\u8fd8\u53ef\u4ee5\u628a\u91cc\u9762\u7684Queue\u6539\u4e3aPipe\u3002<a target=\"_blank\" href=\"https:\/\/zhuanlan.zhihu.com\/p\/38136322\" rel=\"noreferrer noopener\">\u8bfb\u53d6\u591a\u4e2a(\u6d77\u5eb7\\\u5927\u534e)\u7f51\u7edc\u6444\u50cf\u5934\u7684\u89c6\u9891\u6d41 (\u4f7f\u7528opencv-python)\uff0c\u89e3\u51b3\u5b9e\u65f6\u8bfb\u53d6\u5ef6\u8fdf\u95ee\u9898392 \u8d5e\u540c \u00b7 281 \u8bc4\u8bba\u6587\u7ae0<\/a><\/p>\n\n\n\n<p>Pipe\u8fd8\u6709&nbsp;<strong>duplex\u53c2\u6570<\/strong>&nbsp;\u548c&nbsp;<strong>poll() \u65b9\u6cd5<\/strong>&nbsp;\u9700\u8981\u4e86\u89e3\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b duplex==True\uff0c\u82e5\u4e0d\u5f00\u542f\u53cc\u5411\u7ba1\u9053\uff0c\u90a3\u4e48\u4f20\u6570\u636e\u7684\u65b9\u5411\u53ea\u80fd conn1 \u2190 conn2 \u3002conn2.poll()==True \u610f\u5473\u7740\u53ef\u4ee5\u9a6c\u4e0a\u4f7f\u7528 conn2.recv() \u62ff\u5230\u4f20\u8fc7\u6765\u7684\u6570\u636e\u3002conn2.poll(n) \u4f1a\u8ba9\u5b83\u7b49\u5f85n\u79d2\u949f\u518d\u8fdb\u884c\u67e5\u8be2\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from multiprocessing import Pipe\n\nconn1, conn2 = Pipe(duplex=True)  <em># \u5f00\u542f\u53cc\u5411\u7ba1\u9053\uff0c\u7ba1\u9053\u4e24\u7aef\u90fd\u80fd\u5b58\u53d6\u6570\u636e\u3002\u9ed8\u8ba4\u5f00\u542f<\/em>\n<em># <\/em>\nconn1.send('A')\nprint(conn1.poll())  <em># \u4f1aprint\u51fa False\uff0c\u56e0\u4e3a\u6ca1\u6709\u4e1c\u897f\u7b49\u5f85conn1\u53bb\u63a5\u6536<\/em>\nprint(conn2.poll())  <em># \u4f1aprint\u51fa True \uff0c\u56e0\u4e3aconn1 send \u4e86\u4e2a 'A' \u7b49\u7740conn2 \u53bb\u63a5\u6536<\/em>\nprint(conn2.recv(), conn2.poll(2))  <em># \u4f1a\u7b49\u5f852\u79d2\u949f\u518d\u5f00\u59cb\u67e5\u8be2\uff0c\u7136\u540eprint\u51fa 'A False'<\/em><\/code><\/pre>\n\n\n\n<p>\u5c3d\u7ba1\u6211\u4e0b\u9762\u7684\u4f8b\u5b50\u4e0d\u4f1a\u62a5\u9519\uff0c\u4f46\u8fd9\u662f\u56e0\u4e3a\u5b83\u8fc7\u4e8e\u7b80\u5355\uff0c\u6ca1\u6709\u771f\u7684\u5f00\u591a\u7ebf\u7a0b\u53bb\u8dd1\uff0c\u4e5f\u6ca1\u6709\u5199\u5728\u7a0b\u5e8f\u5165\u53e3\u7684if\u5185\u90e8\u3002\u5f88\u591a\u65f6\u5019 Pipe\u8fd0\u884c\u4f1a\u5feb\u4e00\u70b9\uff0c\u4f46\u662f\u5b83\u7684\u529f\u80fd\u592a\u5c11\u4e86\uff0c\u5f97\u7528 Queue\u3002\u6700\u660e\u663e\u7684\u4e00\u4e2a\u533a\u522b\u662f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>conn1, conn2 = multiprocessing.Pipe()  <em># \u7ba1\u9053\u6709\u4e24\u7aef\uff0c\u67d0\u4e00\u7aef\u653e\u5165\u7684\u4e1c\u897f\uff0c\u53ea\u80fd\u5728\u53e6\u4e00\u7aef\u62ff\u5230<\/em>\nqueue = multiprocessing.Queue()        <em># \u961f\u5217\u53ea\u6709\u4e00\u4e2a\uff0c\u653e\u8fdb\u53bb\u7684\u4e1c\u897f\u53ef\u4ee5\u5728\u4efb\u4f55\u5730\u65b9\u62ff\u5230\u3002<\/em><\/code><\/pre>\n\n\n\n<h2>6. \u961f\u5217 Queue<\/h2>\n\n\n\n<p>\u53ef\u4ee5 import queue \u8c03\u7528Python\u5185\u7f6e\u7684\u961f\u5217\uff0c\u5728\u591a\u7ebf\u7a0b\u91cc\u4e5f\u6709\u961f\u5217 from multiprocessing import Queue\u3002\u4e0b\u9762\u63d0\u53ca\u7684\u90fd\u662f\u591a\u7ebf\u7a0b\u7684\u961f\u5217\u3002<\/p>\n\n\n\n<p>\u961f\u5217Queue \u7684\u529f\u80fd\u4e0e\u524d\u9762\u7684\u7ba1\u9053Pipe\u975e\u5e38\u76f8\u4f3c\uff1a\u65e0\u8bba\u4e3b\u8fdb\u7a0b\u6216\u5b50\u8fdb\u7a0b\uff0c\u90fd\u80fd\u8bbf\u95ee\u5230\u961f\u5217\uff0c\u653e\u8fdb\u53bb\u7684\u5bf9\u8c61\u90fd\u7ecf\u8fc7\u4e86\u6df1\u62f7\u8d1d\u3002\u4e0d\u540c\u7684\u662f\uff1a\u7ba1\u9053Pipe\u53ea\u6709\u4e24\u4e2a\u65ad\u5f00\uff0c\u800c\u961f\u5217Queue \u6709\u57fa\u672c\u7684\u961f\u5217\u5c5e\u6027\uff0c\u66f4\u52a0\u7075\u6d3b\uff0c\u8be6\u7ec6\u8bf7\u79fb\u6b65Stack Overflow&nbsp;<a href=\"https:\/\/stackoverflow.com\/questions\/8463008\/multiprocessing-pipe-vs-queue\" target=\"_blank\" rel=\"noreferrer noopener\">Multiprocessing &#8211; Pipe vs Queue<\/a>\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def func1(i):\n    time.sleep(1)\n    print(f'args {i}')\n\ndef run__queue():\n    from multiprocessing import Process, Queue\n\n    queue = Queue(maxsize=4)  <em># the following attribute can call in anywhere<\/em>\n    queue.put(True)\n    queue.put(&#91;0, None, object])  <em># you can put deepcopy thing<\/em>\n    queue.qsize()  <em># the length of queue<\/em>\n    print(queue.get())  <em># First In First Out<\/em>\n    print(queue.get())  <em># First In First Out<\/em>\n    queue.qsize()  <em># the length of queue<\/em>\n\n    process = &#91;Process(target=func1, args=(queue,)),\n               Process(target=func1, args=(queue,)), ]\n    &#91;p.start() for p in process]\n    &#91;p.join() for p in process]\n\nif __name__ =='__main__':\n    run__queue()<\/code><\/pre>\n\n\n\n<p>\u9664\u4e86\u4e0a\u9762\u63d0\u53ca\u7684&nbsp;<a href=\"https:\/\/zhuanlan.zhihu.com\/p\/38136322\">Python\u591a\u7ebf\u7a0b\uff0c\u8bfb\u53d6\u591a\u4e2a(\u6d77\u5eb7\\\u5927\u534e)\u7f51\u7edc\u6444\u50cf\u5934\u7684\u89c6\u9891\u6d41<\/a>&nbsp;\uff0c\u6211\u81ea\u5df1\u5199\u7684\u5f00\u6e90\u7684<a href=\"https:\/\/github.com\/Yonv1943\/ElegantRL\" target=\"_blank\" rel=\"noreferrer noopener\">\u5f3a\u5316\u5b66\u4e60\u5e93\uff1a\u5c0f\u96c5 ElegantRL<\/a>&nbsp;\u4e5f\u4f7f\u7528\u4e86 Queue \u8fdb\u884c\u591aCPU\u591aGPU\u8bad\u7ec3\uff0c\u4e3a\u4e86\u63d0\u901f\uff0c\u6211\u5df2\u7ecf\u628aQueue \u6539\u4e3a Pipe\u3002<\/p>\n\n\n\n<h2>7. \u5171\u4eab\u5185\u5b58 Manager<\/h2>\n\n\n\n<p>\u4e3a\u4e86\u5728Python\u91cc\u9762\u5b9e\u73b0\u591a\u8fdb\u7a0b\u901a\u4fe1\uff0c\u4e0a\u9762\u63d0\u53ca\u7684 Pipe Queue \u628a\u9700\u8981\u901a\u4fe1\u7684\u4fe1\u606f\u4ece\u5185\u5b58\u91cc\u6df1\u62f7\u8d1d\u4e86\u4e00\u4efd\u7ed9\u5176\u4ed6\u7ebf\u7a0b\u4f7f\u7528\uff08\u9700\u8981\u5206\u53d1\u7684\u7ebf\u7a0b\u8d8a\u591a\uff0c\u5176\u5360\u7528\u7684\u5185\u5b58\u8d8a\u591a\uff09\u3002\u800c\u5171\u4eab\u5185\u5b58\u4f1a\u7531\u89e3\u91ca\u5668\u8d1f\u8d23\u7ef4\u62a4\u4e00\u5757\u5171\u4eab\u5185\u5b58\uff08\u800c\u4e0d\u7528\u6df1\u62f7\u8d1d\uff09\uff0c\u8fd9\u5757\u5185\u5b58\u6bcf\u4e2a\u8fdb\u7a0b\u90fd\u80fd\u8bfb\u53d6\u5230\uff0c\u8bfb\u5199\u7684\u65f6\u5019\u9075\u5b88\u7ba1\u7406\uff08\u56e0\u6b64\u4e0d\u8981\u4ee5\u4e3a\u7528\u4e86\u5171\u4eab\u5185\u5b58\u5c31\u4e00\u5b9a\u53d8\u5feb\uff09\u3002<\/p>\n\n\n\n<p>Manager\u53ef\u4ee5\u521b\u5efa\u4e00\u5757\u5171\u4eab\u7684\u5185\u5b58\u533a\u57df\uff0c\u4f46\u662f\u5b58\u5165\u5176\u4e2d\u7684\u6570\u636e\u9700\u8981\u6309\u7167\u7279\u5b9a\u7684\u683c\u5f0f\uff0cValue\u53ef\u4ee5\u4fdd\u5b58\u6570\u503c\uff0cArray\u53ef\u4ee5\u4fdd\u5b58\u6570\u7ec4\uff0c\u5982\u4e0b\u3002\u8fd9\u91cc\u4e0d\u63a8\u8350\u8ba4\u4e3a\u81ea\u5df1\u5199\u4ee3\u7801\u80fd\u529b\u5f31\u7684\u4eba\u5c1d\u8bd5\u3002\u4e0b\u9762\u8fd9\u91cc\u4f8b\u5b50\u6765\u81ea<a href=\"https:\/\/docs.python.org\/3\/library\/multiprocessing.html\" target=\"_blank\" rel=\"noreferrer noopener\">Python\u5b98\u7f51\u7684Document<\/a>\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># https:\/\/docs.python.org\/3\/library\/multiprocessing.html?highlight=multiprocessing%20array#multiprocessing.Array\n\nfrom multiprocessing import Process, Lock\nfrom multiprocessing.sharedctypes import Value, Array\nfrom ctypes import Structure, c_double\n\nclass Point(Structure):\n    _fields_ = &#91;('x', c_double), ('y', c_double)]\n\ndef modify(n, x, s, A):\n    n.value **= 2\n    x.value **= 2\n    s.value = s.value.upper()\n    for a in A:\n        a.x **= 2\n        a.y **= 2\n\nif __name__ == '__main__':\n    lock = Lock()\n\n    n = Value('i', 7)\n    x = Value(c_double, 1.0\/3.0, lock=False)\n    s = Array('c', b'hello world', lock=lock)\n    A = Array(Point, &#91;(1.875,-6.25), (-5.75,2.0), (2.375,9.5)], lock=lock)\n\n    p = Process(target=modify, args=(n, x, s, A))\n    p.start()\n    p.join()\n\n    print(n.value)\n    print(x.value)\n    print(s.value)\n    print(&#91;(a.x, a.y) for a in A])<\/code><\/pre>\n\n\n\n<p><strong>\u6211\u5220\u6389\u4e86Python 3.8 \u7684shared_momery \u4ecb\u7ecd\uff0c\u8fd9\u90e8\u5206\u6709Bug<\/strong><\/p>\n\n\n\n<p>\u4e0b\u6587\u6765\u81ea&nbsp;<a href=\"https:\/\/stackoverflow.com\/a\/65731003\/9293137\" target=\"_blank\" rel=\"noreferrer noopener\">Stack Overflow\uff0c\u95ee\u9898 Shared memory in multiprocessing \u4e0bthuzhf \u7684\u56de\u7b54 2021-01<\/a>&nbsp;\uff1a<\/p>\n\n\n\n<p>For those interested in using Python3.8 &#8216;s&nbsp;<a href=\"https:\/\/docs.python.org\/3\/library\/multiprocessing.shared_memory.html\" target=\"_blank\" rel=\"noreferrer noopener\">shared_memory<\/a>&nbsp;module, it still has a&nbsp;<a href=\"https:\/\/bugs.python.org\/issue38119\" target=\"_blank\" rel=\"noreferrer noopener\">bug<\/a>&nbsp;which hasn&#8217;t been fixed and is affecting Python3.8\/3.9\/3.10 by now (2021-01-15). The bug is about resource tracker destroys shared memory segments when other processes should still have valid access. So take care if you use it in your code.<\/p>\n\n\n\n<p><strong>PyTorch \u4e5f\u6709\u81ea\u5e26\u7684\u591a\u8fdb\u7a0b torch.multiprocessing<\/strong><\/p>\n\n\n\n<p><a href=\"https:\/\/stackoverflow.com\/a\/50873015\/9293137\" target=\"_blank\" rel=\"noreferrer noopener\">How to share a list of tensors in PyTorch multiprocessing? rozyang \u7684\u56de\u7b54<\/a>&nbsp;\uff0c\u975e\u5e38\u7b80\u5355\uff0c\u6838\u5fc3\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import torch.multiprocessing as mp\ntensor.share_memory_()<\/code><\/pre>\n\n\n\n<h2>8. \u56de\u7b54\u8bc4\u8bba\u533a\u7684\u6709\u7528\u95ee\u9898\uff08\u4e0d\u5efa\u8bae\u79c1\u4fe1\uff09<\/h2>\n\n\n\n<p>\u6b63\u6587\u5df2\u7ecf\u7ed3\u675f\uff0c\u6211\u628a\u90e8\u5206<a href=\"https:\/\/github.com\/Yonv1943\/Python\/blob\/master\/Demo\/DEMO_multi_processing.py\" target=\"_blank\" rel=\"noreferrer noopener\">multiprocessing\u7684\u4ee3\u7801\u90fd\u653e\u5728github<\/a>\u3002\u5e0c\u671b\u5927\u5bb6\u80fd\u5199\u51fa\u8ba9\u81ea\u5df1\u6ee1\u610f\u7684\u591a\u7ebf\u7a0b\u3002\u6211\u8bbe\u8ba1\u9ad8\u6027\u80fd\u7684\u591a\u8fdb\u7a0b\u65f6\uff0c\u4f1a\u9075\u5b88\u4ee5\u4e0b\u89c4\u5219\uff1a<\/p>\n\n\n\n<ul><li>\u5c3d\u53ef\u80fd\u5c11\u4f20\u4e00\u70b9\u6570\u636e<\/li><li>\u5c3d\u53ef\u80fd\u51cf\u5c11\u4e3b\u7ebf\u7a0b\u7684\u8d1f\u62c5<\/li><li>\u5c3d\u53ef\u80fd\u4e0d\u8ba9\u67d0\u4e2a\u8fdb\u7a0b\u50bb\u7b49\u7740<\/li><li>\u5c3d\u53ef\u80fd\u51cf\u5c11\u8fdb\u7a0b\u95f4\u901a\u4fe1\u7684\u9891\u7387<\/li><\/ul>\n\n\n\n<h2>9. \u6211\u4e3a\u4f55\u5199\u3010\u5728Python\u4e2d\u4f18\u96c5\u5730\u7528\u591a\u8fdb\u7a0b\u3011\uff1f<\/h2>\n\n\n\n<ul><li>\u6211\u57282019\u5e74\u524d\u6ca1\u6709\u5728\u7f51\u4e0a\u770b\u5230\u8ba9\u6211\u6ee1\u610f\u7684\u540c\u7c7b\u6587\u7ae0\u3002<\/li><li>\u6211\u4e4b\u524d\u7528\u591a\u8fdb\u7a0b\u89e3\u51b3\u8fc7&nbsp;<a href=\"https:\/\/zhuanlan.zhihu.com\/p\/38136322\">\u66fe\u4f0a\u8a00\uff1a\u8bfb\u53d6\u591a\u4e2a(\u6d77\u5eb7\\\u5927\u534e)\u7f51\u7edc\u6444\u50cf\u5934\u7684\u89c6\u9891\u6d41 (\u4f7f\u7528opencv-python)\uff0c\u89e3\u51b3\u5b9e\u65f6\u8bfb\u53d6\u5ef6\u8fdf\u95ee\u9898<\/a>\uff0c\u5c3d\u7ba1\u6211\u4e0d\u559c\u6b22\u79c1\u4fe1\uff0c\u4f46\u8fd8\u662f\u6709\u4eba\u5728\u79c1\u4fe1\u548c\u8bc4\u8bba\u533a<strong>\u8ba8\u8981\u591a\u7ebf\u7a0b<\/strong>\u5b66\u4e60\u8d44\u6599<\/li><li>\u5728<a href=\"https:\/\/doc%3Cb%3Es.pytho%3C\/b%3En.org\/3\/library\/multiprocessing.shared_memory.html\" target=\"_blank\" rel=\"noreferrer noopener\">2019\u5e74\u7684Python3.9\u7684\u65b0\u7279\u6027\uff1a\u771f\u6b63\u7684\u5171\u4eab\u5185\u5b58 shared_memory<\/a>&nbsp;\u505a\u51fa\u6765\u540e\uff0c\u6211\u8ba4\u4e3a\u73b0\u5728\u4ecb\u7ecd\u65f6\u673a\u6210\u719f\u4e86<\/li><li>\u6211\u5f00\u6e90\u7684<a href=\"https:\/\/github.com\/Yonv1943\/ElegantRL\" target=\"_blank\" rel=\"noreferrer noopener\">\u5f3a\u5316\u5b66\u4e60\u5e93\uff1a\u5c0f\u96c5 ElegantRL<\/a>&nbsp;\u4e3a\u4e86\u8ffd\u6c42\u8bad\u7ec3\u901f\u5ea6\uff0c\u4f7f\u7528\u4e86\u591a\u8fdb\u7a0b\u5b9e\u73b0\u4e86\uff1amulti-workers\uff08rollout\uff09\u3001\u5206\u5f00\u91c7\u6837-\u8bad\u7ec3-\u6d4b\u8bd5\u6a21\u5757\u3001\u672a\u6765\u8fd8\u60f3\u8981\u5b9e\u73b0\u9002\u5408\u5f3a\u5316\u5b66\u4e60\u7684multiGPU&#8230; &#8230; \u5728\u79ef\u7d2f\u4e86\u4e00\u4e9bPython\u591a\u8fdb\u7a0b\u7ecf\u9a8c\u540e\uff0c\u6211\u6253\u7b97\u5206\u4eab\u4e00\u4e0b\u3002<\/li><\/ul>\n\n\n\n<p>\u5f00\u6e90\u7684\u6df1\u5ea6\u5f3a\u5316\u5b66\u4e60(DRL)\u7b97\u6cd5\u5e93&nbsp;<a href=\"https:\/\/github.com\/ray-project\/ray\" target=\"_blank\" rel=\"noreferrer noopener\">\u4f2f\u514b\u5229\u7684Ray-project Rllib<\/a>\u8bad\u7ec3\u5feb\uff0c\u4f46\u592a\u590d\u6742\uff0c<a href=\"https:\/\/spinningup.openai.com\/en\/latest\/\" target=\"_blank\" rel=\"noreferrer noopener\">OpenAI\u7684 SpinningUp<\/a>\u7b80\u5355\uff0c\u4f46\u4e0d\u5feb\uff08\u6ca1\u6709\u63d0\u53ca\u7684\u5f00\u6e90\u5e93\u6bd4\u4e0d\u4e0a\u5b83\u4eec\uff0c\u5199\u4e8e2020\u5e74\uff09\u3002\u521a\u597d\u6211\u53c8\u61c2\u4e00\u70b9\u591a\u8fdb\u7a0b\u3001Numpy\u3001\u6df1\u5ea6\u5b66\u4e60\u6846\u67b6\u3001\u6df1\u5ea6\u5f3a\u5316\u5b66\u4e60\u8fd9\u4e9b\u53cc\u5c42\u4f18\u5316\u7b97\u6cd5\uff0c\u6240\u4ee5\u6211\u89c9\u5f97\u81ea\u5df1\u4e5f\u5199\u4e00\u4e2aDRL\u5e93\u96be\u5ea6\u4e0d\u5927\uff0c\u4e8e\u662f\u5f00\u6e90\u4e86<a href=\"https:\/\/github.com\/Yonv1943\/ElegantRL\" target=\"_blank\" rel=\"noreferrer noopener\">\u5f3a\u5316\u5b66\u4e60\u5e93\uff1a\u5c0f\u96c5 ElegantRL<\/a>\u3002\u8ba9\u522b\u4eba\u597d\u597d\u770b\u770b\uff0cDRL\u5e93\u633a\u7b80\u5355\u7684\u4e00\u4e2a\u4e1c\u897f\u5f04\u90a3\u4e48\u590d\u6742\u505a\u4ec0\u4e48\uff1f<\/p>\n\n\n\n<p>\u5c3d\u7ba1\u8fd9\u4e2a\u5e93\u4f1a\u4e00\u76f4\u4fdd\u6301\u6846\u67b6<strong>\u5c0f\u5de7<\/strong>\u3001\u4ee3\u7801<strong>\u4f18\u96c5<\/strong>\u6765\u65b9\u4fbf\u5165\u95e8\u6df1\u5ea6\u5f3a\u5316\u5b66\u4e60\u7684\u4eba\uff0c\u4f46 ElegantRL \u5374\u628a\u8bad\u7ec3\u6548\u7387\u653e\u5728\u9996\u4f4d\uff08\u6b63\u56e0\u5982\u6b64\uff0cElegantRL \u4e0e SpinningUp\u7684\u5b9a\u4f4d\u4e0d\u540c\uff09\uff0c\u6240\u4ee5\u6211\u9700\u8981\u7528Python\u7684\u591a\u8fdb\u7a0b\u6765\u52a0\u901f DRL\u7684\u8bad\u7ec3\u3002\u56e0\u800c\u987a\u4fbf\u5199\u3010\u5728Python\u4e2d\u4f18\u96c5\u5730\u7528\u591a\u8fdb\u7a0b\u3011\u8fd9\u7bc7\u4e1c\u897f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6458\u81ea\u77e5\u4e4e\uff1ahttps:\/\/zhuanlan.zhihu.com\/p\/340657122 Python\u81ea\u5e26\u7684\u591a\u8fdb &hellip; <a href=\"http:\/\/139.9.1.231\/index.php\/2023\/03\/06\/python_multiprosess\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u5728Python\u4e2d\u4f18\u96c5\u5730\u7528\u591a\u8fdb\u7a0b<\/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\/14679"}],"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=14679"}],"version-history":[{"count":5,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/14679\/revisions"}],"predecessor-version":[{"id":14692,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/14679\/revisions\/14692"}],"wp:attachment":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/media?parent=14679"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/categories?post=14679"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/tags?post=14679"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}