{"id":31930,"date":"2023-05-27T15:43:07","date_gmt":"2023-05-27T07:43:07","guid":{"rendered":"https:\/\/docs.pingcode.com\/?p=31930"},"modified":"2023-07-31T01:57:02","modified_gmt":"2023-07-30T17:57:02","slug":"python%e7%bc%96%e7%a8%8b%e4%b8%ad%ef%bc%8c%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8%e5%a4%9a%e8%bf%9b%e7%a8%8b%e5%ae%8c%e6%88%90%e5%a4%9a%e4%bb%bb%e5%8a%a1%e8%bf%9b%e7%a8%8b%e6%9c%89%e5%93%aa%e4%ba%9b","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/31930.html","title":{"rendered":"Python\u7f16\u7a0b\u4e2d\uff0c\u5982\u4f55\u4f7f\u7528\u591a\u8fdb\u7a0b\u5b8c\u6210\u591a\u4efb\u52a1\u8fdb\u7a0b\u6709\u54ea\u4e9b\u6ce8\u610f\u70b9"},"content":{"rendered":"<blockquote class=\"wp-block-quote\">\n<p>Python\u7f16\u7a0b\u4e2d\uff0c\u4f7f\u7528\u591a\u8fdb\u7a0b\u5b8c\u6210\u591a\u4efb\u52a1\u7684\u65b9\u6cd5\uff1a\u4f7f\u7528multiprocessing\u6a21\u5757\u3002\u8fdb\u7a0b\u7684\u6ce8\u610f\u70b9\u6709\uff1a1\u3001\u8fdb\u7a0b\u4e4b\u95f4\u4e0d\u5171\u4eab\u5168\u5c40\u53d8\u91cf\uff1b2\u3001\u4e3b\u8fdb\u7a0b\u4f1a\u7b49\u5f85\u6240\u6709\u7684\u5b50\u8fdb\u7a0b\u6267\u884c\u7ed3\u675f\u518d\u7ed3\u675f\u3002\u8fdb\u7a0b\u4e4b\u95f4\u4e0d\u5171\u4eab\u5168\u5c40\u53d8\u91cf\u662f\u6307\u64cd\u4f5c\u7684\u4e0d\u662f\u540c\u4e00\u4e2a\u8fdb\u7a0b\u91cc\u9762\u7684\u5168\u5c40\u53d8\u91cf\u3002<\/p>\n<\/blockquote>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img fetchpriority=\"high\" alt=\"\" class=\"wp-image-31931\" decoding=\"async\" height=\"576\" sizes=\"(max-width: 1024px) 100vw, 1024px\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2023\/05\/Python\u7f16\u7a0b\u4e2d\uff0c\u5982\u4f55\u4f7f\u7528\u591a\u8fdb\u7a0b\u5b8c\u6210\u591a\u4efb\u52a1-1024x576.png?x-oss-process=image\/auto-orient,1\/format,webp\" srcset=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2023\/05\/Python\u7f16\u7a0b\u4e2d\uff0c\u5982\u4f55\u4f7f\u7528\u591a\u8fdb\u7a0b\u5b8c\u6210\u591a\u4efb\u52a1-1024x576.png?x-oss-process=image\/auto-orient,1\/format,webp 1024w, https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2023\/05\/Python\u7f16\u7a0b\u4e2d\uff0c\u5982\u4f55\u4f7f\u7528\u591a\u8fdb\u7a0b\u5b8c\u6210\u591a\u4efb\u52a1-300x169.png 300w, https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2023\/05\/Python\u7f16\u7a0b\u4e2d\uff0c\u5982\u4f55\u4f7f\u7528\u591a\u8fdb\u7a0b\u5b8c\u6210\u591a\u4efb\u52a1-768x432.png 768w, https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2023\/05\/Python\u7f16\u7a0b\u4e2d\uff0c\u5982\u4f55\u4f7f\u7528\u591a\u8fdb\u7a0b\u5b8c\u6210\u591a\u4efb\u52a1-270x152.png 270w, https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2023\/05\/Python\u7f16\u7a0b\u4e2d\uff0c\u5982\u4f55\u4f7f\u7528\u591a\u8fdb\u7a0b\u5b8c\u6210\u591a\u4efb\u52a1-1536x865.png 1536w, https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2023\/05\/Python\u7f16\u7a0b\u4e2d\uff0c\u5982\u4f55\u4f7f\u7528\u591a\u8fdb\u7a0b\u5b8c\u6210\u591a\u4efb\u52a1.png 1993w\" width=\"1024\"\/><\/figure>\n<\/div>\n<h2 class=\"wp-block-heading\">\u4e00\u3001Python\u7f16\u7a0b\u4e2d\uff0c\u4f7f\u7528\u591a\u8fdb\u7a0b\u5b8c\u6210\u591a\u4efb\u52a1\u7684\u65b9\u6cd5<\/h2>\n<p>Python\u63d0\u4f9b\u4e86multiprocessing\u6a21\u5757\uff0c\u53ef\u4ee5\u7528\u4e8e\u5728\u5355\u4e2a\u8ba1\u7b97\u673a\u4e0a\u521b\u5efa\u591a\u4e2a\u8fdb\u7a0b\uff0c\u4ece\u800c\u5b9e\u73b0\u591a\u4efb\u52a1\u5904\u7406\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528multiprocessing\u6a21\u5757\u521b\u5efa\u591a\u8fdb\u7a0b\u6765\u5b8c\u6210\u591a\u4efb\u52a1\u3002<\/p>\n<p><strong>\u4ee3\u7801\uff1a<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>import multiprocessing\nimport time\n\n# \u8df3\u821e\u4efb\u52a1\ndef dance():\n    for i in range(5):\n        print(\"\u8df3\u821e\u4e2d...\")\n        time.sleep(0.2)\n\n# \u5531\u6b4c\u4efb\u52a1\ndef sing():\n    for i in range(5):\n        print(\"\u5531\u6b4c\u4e2d...\")\n        time.sleep(0.2)\n\nif __name__ == '__main__':\n    # \u521b\u5efa\u8df3\u821e\u7684\u5b50\u8fdb\u7a0b\n    # group: \u8868\u793a\u8fdb\u7a0b\u7ec4\uff0c\u76ee\u524d\u53ea\u80fd\u4f7f\u7528None\n    # target: \u8868\u793a\u6267\u884c\u7684\u76ee\u6807\u4efb\u52a1\u540d(\u51fd\u6570\u540d\u3001\u65b9\u6cd5\u540d)\n    # name: \u8fdb\u7a0b\u540d\u79f0, \u9ed8\u8ba4\u662fProcess-1, .....\n    dance_process = multiprocessing.Process(target=dance, name=\"myprocess1\")\n    sing_process = multiprocessing.Process(target=sing)\n    # \u542f\u52a8\u5b50\u8fdb\u7a0b\u6267\u884c\u5bf9\u5e94\u7684\u4efb\u52a1\n    dance_process.start()\n    sing_process.start()<\/code><\/pre>\n<p><strong>\u6267\u884c\u7ed3\u679c\uff1a<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>\u5531\u6b4c\u4e2d...\n\u8df3\u821e\u4e2d...\n\u5531\u6b4c\u4e2d...\n\u8df3\u821e\u4e2d...\n\u5531\u6b4c\u4e2d...\n\u8df3\u821e\u4e2d...\n\u5531\u6b4c\u4e2d...\n\u8df3\u821e\u4e2d...\n\u5531\u6b4c\u4e2d...\n\u8df3\u821e\u4e2d...<\/code><\/pre>\n<h2 class=\"wp-block-heading\">\u4e8c\u3001\u8fdb\u7a0b\u6709\u54ea\u4e9b\u6ce8\u610f\u70b9<\/h2>\n<h3 class=\"wp-block-heading\">1\u3001\u8fdb\u7a0b\u4e4b\u95f4\u4e0d\u5171\u4eab\u5168\u5c40\u53d8\u91cf<\/h3>\n<p><strong>\u4ee3\u7801\uff1a<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>import multiprocessing\nimport time\n# \u5b9a\u4e49\u5168\u5c40\u53d8\u91cf\ng_list = list()\n# \u6dfb\u52a0\u6570\u636e\u7684\u4efb\u52a1\ndef add_data():\n    for i in range(5):\n        g_list.append(i)\n        print(\"add:\", i)\n        time.sleep(0.2)\n\n    # \u4ee3\u7801\u6267\u884c\u5230\u6b64\uff0c\u8bf4\u660e\u6570\u636e\u6dfb\u52a0\u5b8c\u6210\n    print(\"add_data:\", g_list)\n\ndef read_data():\n    print(\"read_data\", g_list)\n\nif __name__ == '__main__':\n    # \u521b\u5efa\u6dfb\u52a0\u6570\u636e\u7684\u5b50\u8fdb\u7a0b\n    add_data_process = multiprocessing.Process(target=add_data)\n    # \u521b\u5efa\u8bfb\u53d6\u6570\u636e\u7684\u5b50\u8fdb\u7a0b\n    read_data_process = multiprocessing.Process(target=read_data)\n    # \u542f\u52a8\u5b50\u8fdb\u7a0b\u6267\u884c\u5bf9\u5e94\u7684\u4efb\u52a1\n    add_data_process.start()\n    # \u4e3b\u8fdb\u7a0b\u7b49\u5f85\u6dfb\u52a0\u6570\u636e\u7684\u5b50\u8fdb\u7a0b\u6267\u884c\u5b8c\u6210\u4ee5\u540e\u7a0b\u5e8f\u518d\u7ee7\u7eed\u5f80\u4e0b\u6267\u884c\uff0c\u8bfb\u53d6\u6570\u636e\n    add_data_process.join()\n    read_data_process.start()\n\n    print(\"main:\", g_list)\n\n    # \u603b\u7ed3: \u591a\u8fdb\u7a0b\u4e4b\u95f4\u4e0d\u5171\u4eab\u5168\u5c40\u53d8\u91cf<\/code><\/pre>\n<p><strong>\u6267\u884c\u7ed3\u679c\uff1a<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>add: 0<br\/>add: 1<br\/>add: 2<br\/>add: 3<br\/>add: 4<br\/>add_data: [0, 1, 2, 3, 4]<br\/>main: []<br\/>read_data []<\/code><\/pre>\n<p>\u521b\u5efa\u5b50\u8fdb\u7a0b\u4f1a\u5bf9\u4e3b\u8fdb\u7a0b\u8d44\u6e90\u8fdb\u884c\u62f7\u8d1d\uff0c\u4e5f\u5c31\u662f\u8bf4\u5b50\u8fdb\u7a0b\u662f\u4e3b\u8fdb\u7a0b\u7684\u4e00\u4e2a\u526f\u672c\uff0c\u597d\u6bd4\u662f\u4e00\u5bf9\u53cc\u80de\u80ce\uff0c\u4e4b\u6240\u4ee5\u8fdb\u7a0b\u4e4b\u95f4\u4e0d\u5171\u4eab\u5168\u5c40\u53d8\u91cf\uff0c\u662f\u56e0\u4e3a\u64cd\u4f5c\u7684\u4e0d\u662f\u540c\u4e00\u4e2a\u8fdb\u7a0b\u91cc\u9762\u7684\u5168\u5c40\u53d8\u91cf\uff0c\u53ea\u4e0d\u8fc7\u4e0d\u540c\u8fdb\u7a0b\u91cc\u9762\u7684\u5168\u5c40\u53d8\u91cf\u540d\u5b57\u76f8\u540c\u800c\u5df2\u3002<\/p>\n<h3 class=\"wp-block-heading\">2\u3001\u4e3b\u8fdb\u7a0b\u4f1a\u7b49\u5f85\u6240\u6709\u7684\u5b50\u8fdb\u7a0b\u6267\u884c\u7ed3\u675f\u518d\u7ed3\u675f<\/h3>\n<p>\u5047\u5982\u6211\u4eec\u73b0\u5728\u521b\u5efa\u4e00\u4e2a\u5b50\u8fdb\u7a0b\uff0c\u8fd9\u4e2a\u5b50\u8fdb\u7a0b\u6267\u884c\u5b8c\u5927\u6982\u9700\u89812\u79d2\u949f\uff0c\u73b0\u5728\u8ba9\u4e3b\u8fdb\u7a0b\u6267\u884c0.5\u79d2\u949f\u5c31\u9000\u51fa\u7a0b\u5e8f\uff0c\u67e5\u770b\u4e00\u4e0b\u6267\u884c\u7ed3\u679c\uff0c<strong>\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>import multiprocessing\nimport time\n# \u5b9a\u4e49\u8fdb\u7a0b\u6240\u9700\u8981\u6267\u884c\u7684\u4efb\u52a1\ndef task():\n    for i in range(10):\n        print(\"\u4efb\u52a1\u6267\u884c\u4e2d...\")\n        time.sleep(0.2)\n\nif __name__ == '__main__':\n    # \u521b\u5efa\u5b50\u8fdb\u7a0b\n    sub_process = multiprocessing.Process(target=task)\n    sub_process.start()\n\n    # \u4e3b\u8fdb\u7a0b\u5ef6\u65f60.5\u79d2\u949f\n    time.sleep(0.5)\n    print(\"over\")\n    exit()\n    # \u603b\u7ed3\uff1a\u4e3b\u8fdb\u7a0b\u4f1a\u7b49\u5f85\u6240\u6709\u7684\u5b50\u8fdb\u7a0b\u6267\u884c\u5b8c\u6210\u4ee5\u540e\u7a0b\u5e8f\u518d\u9000\u51fa<\/code><\/pre>\n<p><strong>\u6267\u884c\u7ed3\u679c\uff1a<\/strong><\/p>\n<pre class=\"wp-block-preformatted\">\u4efb\u52a1\u6267\u884c\u4e2d...\n\u4efb\u52a1\u6267\u884c\u4e2d...\n\u4efb\u52a1\u6267\u884c\u4e2d...\nover\n\u4efb\u52a1\u6267\u884c\u4e2d...\n\u4efb\u52a1\u6267\u884c\u4e2d...\n\u4efb\u52a1\u6267\u884c\u4e2d...\n\u4efb\u52a1\u6267\u884c\u4e2d...\n\u4efb\u52a1\u6267\u884c\u4e2d...\n\u4efb\u52a1\u6267\u884c\u4e2d...\n\u4efb\u52a1\u6267\u884c\u4e2d...<\/pre>\n<p><strong>\u8bf4\u660e\uff1a<\/strong>\u901a\u8fc7\u4e0a\u9762\u4ee3\u7801\u7684\u6267\u884c\u7ed3\u679c\uff0c\u6211\u4eec\u53ef\u4ee5\u5f97\u77e5\uff1a\u4e3b\u8fdb\u7a0b\u4f1a\u7b49\u5f85\u6240\u6709\u7684\u5b50\u8fdb\u7a0b\u6267\u884c\u7ed3\u675f\u518d\u7ed3\u675f\u3002<\/p>\n<p>\u5047\u5982\u6211\u4eec\u5c31\u8ba9\u4e3b\u8fdb\u7a0b\u6267\u884c0.5\u79d2\u949f\uff0c\u5b50\u8fdb\u7a0b\u5c31\u9500\u6bc1\u4e0d\u518d\u6267\u884c\uff0c\u90a3\u600e\u4e48\u529e\u5462\uff1f\u6211\u4eec\u53ef\u4ee5\u8bbe\u7f6e\u5b88\u62a4\u4e3b\u8fdb\u7a0b\u6216\u8005\u5728\u4e3b\u8fdb\u7a0b\u9000\u51fa\u4e4b\u524d\u8ba9\u5b50\u8fdb\u7a0b\u9500\u6bc1\uff1a<\/p>\n<ul>\n<li><strong>\u5b88\u62a4\u4e3b\u8fdb\u7a0b\uff1a<\/strong>\u5b88\u62a4\u4e3b\u8fdb\u7a0b\u5c31\u662f\u4e3b\u8fdb\u7a0b\u9000\u51fa\u5b50\u8fdb\u7a0b\u9500\u6bc1\u4e0d\u518d\u6267\u884c<\/li>\n<li><strong>\u5b50\u8fdb\u7a0b\u9500\u6bc1\uff1a<\/strong>\u5b50\u8fdb\u7a0b\u6267\u884c\u7ed3\u675f<\/li>\n<\/ul>\n<p>\u4fdd\u8bc1\u4e3b\u8fdb\u7a0b\u6b63\u5e38\u9000\u51fa\u7684<strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>import multiprocessing\nimport time\n# \u5b9a\u4e49\u8fdb\u7a0b\u6240\u9700\u8981\u6267\u884c\u7684\u4efb\u52a1\ndef task():\n    for i in range(10):\n        print(\"\u4efb\u52a1\u6267\u884c\u4e2d...\")\n        time.sleep(0.2)\n\nif __name__ == '__main__':\n    # \u521b\u5efa\u5b50\u8fdb\u7a0b\n    sub_process = multiprocessing.Process(target=task)\n    # \u8bbe\u7f6e\u5b88\u62a4\u4e3b\u8fdb\u7a0b\uff0c\u4e3b\u8fdb\u7a0b\u9000\u51fa\u5b50\u8fdb\u7a0b\u76f4\u63a5\u9500\u6bc1\uff0c\u5b50\u8fdb\u7a0b\u7684\u751f\u547d\u5468\u671f\u4f9d\u8d56\u4e0e\u4e3b\u8fdb\u7a0b\n    # sub_process.daemon = True\n    sub_process.start()\n\n    time.sleep(0.5)\n    print(\"over\")\n    # \u8ba9\u5b50\u8fdb\u7a0b\u9500\u6bc1\n    sub_process.terminate()\n    exit()\n\n    # \u603b\u7ed3\uff1a\u4e3b\u8fdb\u7a0b\u4f1a\u7b49\u5f85\u6240\u6709\u7684\u5b50\u8fdb\u7a0b\u6267\u884c\u5b8c\u6210\u4ee5\u540e\u7a0b\u5e8f\u518d\u9000\u51fa\n    # \u5982\u679c\u60f3\u8981\u4e3b\u8fdb\u7a0b\u9000\u51fa\u5b50\u8fdb\u7a0b\u9500\u6bc1\uff0c\u53ef\u4ee5\u8bbe\u7f6e\u5b88\u62a4\u4e3b\u8fdb\u7a0b\u6216\u8005\u5728\u4e3b\u8fdb\u7a0b\u9000\u51fa\u4e4b\u524d\u8ba9\u5b50\u8fdb\u7a0b\u9500\u6bc1<\/code><\/pre>\n<p><strong>\u6267\u884c\u7ed3\u679c\uff1a<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>\u4efb\u52a1\u6267\u884c\u4e2d...\n\u4efb\u52a1\u6267\u884c\u4e2d...\n\u4efb\u52a1\u6267\u884c\u4e2d...\nover<\/code><\/pre>\n<h2 class=\"wp-block-heading\">\u4e09\u3001Python\u591a\u7ebf\u7a0b\u7684\u901a\u4fe1<\/h2>\n<p>\u8fdb\u7a0b\u662f\u7cfb\u7edf\u72ec\u7acb\u8c03\u5ea6\u6838\u5206\u914d\u7cfb\u7edf\u8d44\u6e90\uff08CPU\u3001\u5185\u5b58\uff09\u7684\u57fa\u672c\u5355\u4f4d\uff0c\u8fdb\u7a0b\u4e4b\u95f4\u662f\u76f8\u4e92\u72ec\u7acb\u7684\uff0c\u6bcf\u542f\u52a8\u4e00\u4e2a\u65b0\u7684\u8fdb\u7a0b\u76f8\u5f53\u4e8e\u628a\u6570\u636e\u8fdb\u884c\u4e86\u4e00\u6b21\u514b\u9686\uff0c\u5b50\u8fdb\u7a0b\u91cc\u7684\u6570\u636e\u4fee\u6539\u65e0\u6cd5\u5f71\u54cd\u5230\u4e3b\u8fdb\u7a0b\u4e2d\u7684\u6570\u636e\uff0c\u4e0d\u540c\u5b50\u8fdb\u7a0b\u4e4b\u95f4\u7684\u6570\u636e\u4e5f\u4e0d\u80fd\u5171\u4eab\uff0c\u8fd9\u662f\u591a\u8fdb\u7a0b\u5728\u4f7f\u7528\u4e2d\u4e0e\u591a\u7ebf\u7a0b\u6700\u660e\u663e\u7684\u533a\u522b\u3002\u4f46\u662f\u96be\u9053Python\u591a\u8fdb\u7a0b\u4e2d\u95f4\u96be\u9053\u5c31\u662f\u5b64\u7acb\u7684\u5417\uff1f\u5f53\u7136\u4e0d\u662f\uff0cpython\u4e5f\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\u4e86\u591a\u8fdb\u7a0b\u4e2d\u95f4\u7684\u901a\u4fe1\u548c\u6570\u636e\u5171\u4eab\uff08\u53ef\u4ee5\u4fee\u6539\u4e00\u4efd\u6570\u636e\uff09\u3002<\/p>\n<h3 class=\"wp-block-heading\">1\u3001\u8fdb\u7a0b\u5bf9\u5217Queue<\/h3>\n<p>Queue\u5728\u591a\u7ebf\u7a0b\u4e2d\u4e5f\u8bf4\u5230\u8fc7\uff0c\u5728\u751f\u6210\u8005\u6d88\u8d39\u8005\u6a21\u5f0f\u4e2d\u4f7f\u7528\uff0c\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\uff0c\u662f\u751f\u4ea7\u8005\u548c\u6d88\u8d39\u8005\u4e2d\u95f4\u7684\u6570\u636e\u7ba1\u9053\uff0c\u90a3\u5728python\u591a\u8fdb\u7a0b\u4e2d\uff0c\u5b83\u5176\u5b9e\u5c31\u662f\u8fdb\u7a0b\u4e4b\u95f4\u7684\u6570\u636e\u7ba1\u9053\uff0c\u5b9e\u73b0\u8fdb\u7a0b\u901a\u4fe1\u3002<\/p>\n<pre class=\"wp-block-code\"><code>from multiprocessing import Process,Queue\n\n\ndef fun1(q,i):\n    print('\u5b50\u8fdb\u7a0b%s \u5f00\u59cbput\u6570\u636e' %i)\n    q.put('\u6211\u662f%s \u901a\u8fc7Queue\u901a\u4fe1' %i)\n\nif __name__ == '__main__':\n    q = Queue()\n\n    process_list = []\n    for i in range(3):\n        p = Process(target=fun1,args=(q,i,))  #\u6ce8\u610fargs\u91cc\u9762\u8981\u628aq\u5bf9\u8c61\u4f20\u7ed9\u6211\u4eec\u8981\u6267\u884c\u7684\u65b9\u6cd5\uff0c\u8fd9\u6837\u5b50\u8fdb\u7a0b\u624d\u80fd\u548c\u4e3b\u8fdb\u7a0b\u7528Queue\u6765\u901a\u4fe1\n        p.start()\n        process_list.append(p)\n\n    for i in process_list:\n        p.join()\n\n    print('\u4e3b\u8fdb\u7a0b\u83b7\u53d6Queue\u6570\u636e')\n    print(q.get())\n    print(q.get())\n    print(q.get())\n    print('\u7ed3\u675f\u6d4b\u8bd5')<\/code><\/pre>\n<p><strong>\u7ed3\u679c\uff1a<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>\u5b50\u8fdb\u7a0b0 \u5f00\u59cbput\u6570\u636e\n\u5b50\u8fdb\u7a0b1 \u5f00\u59cbput\u6570\u636e\n\u5b50\u8fdb\u7a0b2 \u5f00\u59cbput\u6570\u636e\n\u4e3b\u8fdb\u7a0b\u83b7\u53d6Queue\u6570\u636e\n\u6211\u662f0 \u901a\u8fc7Queue\u901a\u4fe1\n\u6211\u662f1 \u901a\u8fc7Queue\u901a\u4fe1\n\u6211\u662f2 \u901a\u8fc7Queue\u901a\u4fe1\n\u7ed3\u675f\u6d4b\u8bd5\n\nProcess finished with exit code 0<\/code><\/pre>\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u7ed3\u679c\u53ef\u4ee5\u770b\u5230\u6211\u4eec\u4e3b\u8fdb\u7a0b\u4e2d\u53ef\u4ee5\u901a\u8fc7Queue\u83b7\u53d6\u5b50\u8fdb\u7a0b\u4e2dput\u7684\u6570\u636e\uff0c\u5b9e\u73b0\u8fdb\u7a0b\u95f4\u7684\u901a\u4fe1\u3002<\/p>\n<h3 class=\"wp-block-heading\">2\u3001\u7ba1\u9053Pipe<\/h3>\n<p>\u7ba1\u9053Pipe\u548cQueue\u7684\u4f5c\u7528\u5927\u81f4\u5dee\u4e0d\u591a\uff0c\u4e5f\u662f\u5b9e\u73b0\u8fdb\u7a0b\u95f4\u7684\u901a\u4fe1\u3002<\/p>\n<pre class=\"wp-block-code\"><code>from multiprocessing import Process, Pipe\ndef fun1(conn):\n    print('\u5b50\u8fdb\u7a0b\u53d1\u9001\u6d88\u606f\uff1a')\n    conn.send('\u4f60\u597d\u4e3b\u8fdb\u7a0b')\n    print('\u5b50\u8fdb\u7a0b\u63a5\u53d7\u6d88\u606f\uff1a')\n    print(conn.recv())\n    conn.close()\n\nif __name__ == '__main__':\n    conn1, conn2 = Pipe() #\u5173\u952e\u70b9\uff0cpipe\u5b9e\u4f8b\u5316\u751f\u6210\u4e00\u4e2a\u53cc\u5411\u7ba1\n    p = Process(target=fun1, args=(conn2,)) #conn2\u4f20\u7ed9\u5b50\u8fdb\u7a0b\n    p.start()\n    print('\u4e3b\u8fdb\u7a0b\u63a5\u53d7\u6d88\u606f\uff1a')\n    print(conn1.recv())\n    print('\u4e3b\u8fdb\u7a0b\u53d1\u9001\u6d88\u606f\uff1a')\n    conn1.send(\"\u4f60\u597d\u5b50\u8fdb\u7a0b\")\n    p.join()\n    print('\u7ed3\u675f\u6d4b\u8bd5')<\/code><\/pre>\n<p><strong>\u7ed3\u679c\uff1a<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>\u4e3b\u8fdb\u7a0b\u63a5\u53d7\u6d88\u606f\uff1a\n\u5b50\u8fdb\u7a0b\u53d1\u9001\u6d88\u606f\uff1a\n\u5b50\u8fdb\u7a0b\u63a5\u53d7\u6d88\u606f\uff1a\n\u4f60\u597d\u4e3b\u8fdb\u7a0b\n\u4e3b\u8fdb\u7a0b\u53d1\u9001\u6d88\u606f\uff1a\n\u4f60\u597d\u5b50\u8fdb\u7a0b\n\u7ed3\u675f\u6d4b\u8bd5\n\nProcess finished with exit code 0<\/code><\/pre>\n<p>\u4e0a\u9762\u53ef\u4ee5\u770b\u5230\u4e3b\u8fdb\u7a0b\u548c\u5b50\u8fdb\u7a0b\u53ef\u4ee5\u76f8\u4e92\u53d1\u9001\u6d88\u606f\u3002<\/p>\n<h3 class=\"wp-block-heading\">3\u3001Managers<\/h3>\n<p>Queue\u548cPipe\u53ea\u662f\u5b9e\u73b0\u4e86\u6570\u636e\u4ea4\u4e92\uff0c\u5e76\u6ca1\u5b9e\u73b0\u6570\u636e\u5171\u4eab\uff0c\u5373\u4e00\u4e2a\u8fdb\u7a0b\u53bb\u66f4\u6539\u53e6\u4e00\u4e2a\u8fdb\u7a0b\u7684\u6570\u636e\u3002\u90a3\u4e48\u5c31\u8981\u7528\u5230Managers\u3002<\/p>\n<pre class=\"wp-block-code\"><code>from multiprocessing import Process, Manager\n\ndef fun1(dic,lis,index):\n\n    dic[index] = 'a'\n    dic['2'] = 'b'    \n    lis.append(index)    #[0,1,2,3,4,0,1,2,3,4,5,6,7,8,9]\n    #print(l)\n\nif __name__ == '__main__':\n    with Manager() as manager:\n        dic = manager.dict()#\u6ce8\u610f\u5b57\u5178\u7684\u58f0\u660e\u65b9\u5f0f\uff0c\u4e0d\u80fd\u76f4\u63a5\u901a\u8fc7{}\u6765\u5b9a\u4e49\n        l = manager.list(range(5))#[0,1,2,3,4]\n\n        process_list = []\n        for i in range(10):\n            p = Process(target=fun1, args=(dic,l,i))\n            p.start()\n            process_list.append(p)\n\n        for res in process_list:\n            res.join()\n        print(dic)\n        print(l)<\/code><\/pre>\n<p><strong>\u7ed3\u679c\uff1a<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>{0: 'a', '2': 'b', 3: 'a', 1: 'a', 2: 'a', 4: 'a', 5: 'a', 7: 'a', 6: 'a', 8: 'a', 9: 'a'}\n[0, 1, 2, 3, 4, 0, 3, 1, 2, 4, 5, 7, 6, 8, 9]<\/code><\/pre>\n<p>\u53ef\u4ee5\u770b\u5230\u4e3b\u8fdb\u7a0b\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5b57\u5178\u548c\u4e00\u4e2a\u5217\u8868\uff0c\u5728\u5b50\u8fdb\u7a0b\u4e2d\uff0c\u53ef\u4ee5\u6dfb\u52a0\u548c\u4fee\u6539\u5b57\u5178\u7684\u5185\u5bb9\uff0c\u5728\u5217\u8868\u4e2d\u63d2\u5165\u65b0\u7684\u6570\u636e\uff0c\u5b9e\u73b0\u8fdb\u7a0b\u95f4\u7684\u6570\u636e\u5171\u4eab\uff0c\u5373\u53ef\u4ee5\u5171\u540c\u4fee\u6539\u540c\u4e00\u4efd\u6570\u636e\u3002<\/p>\n<p><strong><em>\u5ef6\u4f38\u9605\u8bfb1\uff1aPython\u534f\u7a0b\u7b80\u4ecb<\/em><\/strong><\/p>\n<p>\u534f\u7a0b\uff0c\u53c8\u79f0\u5fae\u7ebf\u7a0b\uff0c\u7ea4\u7a0b\uff0c\u82f1\u6587\u540dCoroutine\u3002\u534f\u7a0b\u7684\u4f5c\u7528\u662f\u5728\u6267\u884c\u51fd\u6570A\u65f6\u53ef\u4ee5\u968f\u65f6\u4e2d\u65ad\u53bb\u6267\u884c\u51fd\u6570B\uff0c\u7136\u540e\u4e2d\u65ad\u51fd\u6570B\u7ee7\u7eed\u6267\u884c\u51fd\u6570A\uff08\u53ef\u4ee5\u81ea\u7531\u5207\u6362\uff09\u3002\u4f46\u8fd9\u4e00\u8fc7\u7a0b\u5e76\u4e0d\u662f\u51fd\u6570\u8c03\u7528\uff0c\u8fd9\u4e00\u6574\u4e2a\u8fc7\u7a0b\u770b\u4f3c\u50cf\u591a\u7ebf\u7a0b\uff0c\u7136\u800c\u534f\u7a0b\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u6267\u884c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u7f16\u7a0b\u4e2d\uff0c\u4f7f\u7528\u591a\u8fdb\u7a0b\u5b8c\u6210\u591a\u4efb\u52a1\u7684\u65b9\u6cd5\uff1a\u4f7f\u7528multiprocessing\u6a21\u5757\u3002\u8fdb\u7a0b\u7684\u6ce8\u610f\u70b9\u6709\uff1a1\u3001\u8fdb [&hellip;]","protected":false},"author":1,"featured_media":31931,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[119],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/31930"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=31930"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/31930\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/31931"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=31930"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=31930"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=31930"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}