{"id":1245,"date":"2020-01-14T22:10:00","date_gmt":"2020-01-14T14:10:00","guid":{"rendered":"https:\/\/www.yumefx.com\/?p=1245"},"modified":"2021-03-03T18:02:39","modified_gmt":"2021-03-03T10:02:39","slug":"python%e5%a4%9a%e7%ba%bf%e7%a8%8b%e4%b8%8e%e5%a4%9a%e8%bf%9b%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/www.yumefx.com\/?p=1245","title":{"rendered":"python\u591a\u7ebf\u7a0b\u4e0e\u591a\u8fdb\u7a0b"},"content":{"rendered":"\n<h1 style=\"text-align: center;\">\u4ecb\u7ecd<\/h1>\n<p>\u73b0\u4ee3\u64cd\u4f5c\u7cfb\u7edf\uff0c\u65e0\u8bba\u662f\u7535\u8111\u64cd\u4f5c\u7cfb\u7edf\uff08Mac OS\u3001Linux\u3001Windows\uff09\u8fd8\u662f\u624b\u673a\u64cd\u4f5c\u7cfb\u7edf\uff08IOS\u3001Android\uff09\uff0c\u90fd\u662f\u652f\u6301\u201c\u591a\u4efb\u52a1\u201d\u7684\u64cd\u4f5c\u7cfb\u7edf\u3002<\/p>\n<p>\u6240\u8c13\u591a\u4efb\u52a1\uff0c\u5c31\u662f\u64cd\u4f5c\u7cfb\u7edf\u53ef\u4ee5\u540c\u65f6\u8fd0\u884c\u591a\u4e2a\u4efb\u52a1\u3002\u4f8b\u5982\uff0c\u4e00\u8fb9\u542c\u7740\u7f51\u6613\u4e91\u97f3\u4e50\uff0c\u4e00\u8fb9\u7528\u8c37\u6b4c\u67e5\u8d44\u6599\uff0c\u4e00\u8fb9\u7528word\u5199\u7740\u6587\u7ae0\uff0c\u8fd9\u5c31\u662f\u591a\u4efb\u52a1\uff0c\u81f3\u5c11\u540c\u65f6\u67093\u4e2a\u4efb\u52a1\u6b63\u5728\u8fd0\u884c\u3002\u8fd8\u6709\u5f88\u591a\u670d\u52a1\u6084\u6084\u5730\u5728\u540e\u53f0\u540c\u65f6\u8fd0\u884c\u7740\uff0c\u53ea\u662f\u684c\u9762\u4e0a\u6ca1\u6709\u663e\u793a\u800c\u5df2\u3002<\/p>\n<p>\u73b0\u5728\uff0c\u591a\u6838CPU\u5df2\u7ecf\u975e\u5e38\u666e\u53ca\u4e86\uff0c\u4f46\u662f\uff0c\u5373\u4f7f\u8fc7\u53bb\u7684\u5355\u6838CPU\uff0c\u4e5f\u53ef\u4ee5\u6267\u884c\u591a\u4efb\u52a1\u3002\u7531\u4e8eCPU\u6267\u884c\u4ee3\u7801\u90fd\u662f\u987a\u5e8f\u6267\u884c\u7684\uff0c\u90a3\u4e48\uff0c\u5355\u6838CPU\u662f\u600e\u4e48\u6267\u884c\u591a\u4efb\u52a1\u7684\u5462\uff1f<\/p>\n<p>\u7b54\u6848\u5c31\u662f\u64cd\u4f5c\u7cfb\u7edf\u8f6e\u6d41\u8ba9\u5404\u4e2a\u4efb\u52a1\u4ea4\u66ff\u6267\u884c\uff0c\u4efb\u52a11\u6267\u884c0.01\u79d2\uff0c\u5207\u6362\u5230\u4efb\u52a12\uff0c\u4efb\u52a12\u6267\u884c0.01\u79d2\uff0c\u518d\u5207\u6362\u5230\u4efb\u52a13\uff0c\u6267\u884c0.01\u79d2\u2026\u2026\u8fd9\u6837\u53cd\u590d\u6267\u884c\u4e0b\u53bb\u3002<\/p>\n<p>\u8868\u9762\u4e0a\u770b\uff0c\u6bcf\u4e2a\u4efb\u52a1\u90fd\u662f\u4ea4\u66ff\u6267\u884c\u7684\uff0c\u4f46\u662f\uff0c\u7531\u4e8eCPU\u7684\u6267\u884c\u901f\u5ea6\u5b9e\u5728\u662f\u592a\u5feb\u4e86\uff0c\u6211\u4eec\u611f\u89c9\u5c31\u50cf\u6240\u6709\u4efb\u52a1\u90fd\u5728\u540c\u65f6\u6267\u884c\u4e00\u6837\u3002<\/p>\n<p>\u771f\u6b63\u7684\u5e76\u884c\u6267\u884c\u591a\u4efb\u52a1\u53ea\u80fd\u5728\u591a\u6838CPU\u4e0a\u5b9e\u73b0\uff0c\u4f46\u662f\uff0c\u7531\u4e8e\u4efb\u52a1\u6570\u91cf\u8fdc\u8fdc\u591a\u4e8eCPU\u7684\u6838\u5fc3\u6570\u91cf\uff0c\u6240\u4ee5\uff0c\u64cd\u4f5c\u7cfb\u7edf\u4e5f\u4f1a\u81ea\u52a8\u628a\u5f88\u591a\u4efb\u52a1\u8f6e\u6d41\u8c03\u5ea6\u5230\u6bcf\u4e2a\u6838\u5fc3\u4e0a\u6267\u884c\u3002<\/p>\n<p>\u5bf9\u4e8e\u64cd\u4f5c\u7cfb\u7edf\u6765\u8bf4\uff0c\u4e00\u4e2a\u4efb\u52a1\u5c31\u662f\u4e00\u4e2a\u8fdb\u7a0b\uff08Process\uff09\uff0c\u6bd4\u5982\u6253\u5f00\u4e00\u4e2a\u6d4f\u89c8\u5668\u5c31\u662f\u542f\u52a8\u4e00\u4e2a\u6d4f\u89c8\u5668\u8fdb\u7a0b\uff0c\u6253\u5f00\u4e00\u4e2a\u8bb0\u4e8b\u672c\u5c31\u542f\u52a8\u4e86\u4e00\u4e2a\u8bb0\u4e8b\u672c\u8fdb\u7a0b\uff0c\u6253\u5f00\u4e24\u4e2a\u8bb0\u4e8b\u672c\u5c31\u542f\u52a8\u4e86\u4e24\u4e2a\u8bb0\u4e8b\u672c\u8fdb\u7a0b\uff0c\u6253\u5f00\u4e00\u4e2aWord\u5c31\u542f\u52a8\u4e86\u4e00\u4e2aWord\u8fdb\u7a0b\u3002<\/p>\n<p>\u6709\u4e9b\u8fdb\u7a0b\u8fd8\u4e0d\u6b62\u540c\u65f6\u5e72\u4e00\u4ef6\u4e8b\uff0c\u6bd4\u5982Word\uff0c\u5b83\u53ef\u4ee5\u540c\u65f6\u8fdb\u884c\u6253\u5b57\u3001\u62fc\u5199\u68c0\u67e5\u3001\u6253\u5370\u7b49\u4e8b\u60c5\u3002\u5728\u4e00\u4e2a\u8fdb\u7a0b\u5185\u90e8\uff0c\u8981\u540c\u65f6\u5e72\u591a\u4ef6\u4e8b\uff0c\u5c31\u9700\u8981\u540c\u65f6\u8fd0\u884c\u591a\u4e2a\u201c\u5b50\u4efb\u52a1\u201d\uff0c\u6211\u4eec\u628a\u8fdb\u7a0b\u5185\u7684\u8fd9\u4e9b\u201c\u5b50\u4efb\u52a1\u201d\u79f0\u4e3a\u7ebf\u7a0b\uff08Thread\uff09\u3002<\/p>\n<p>\u7531\u4e8e\u6bcf\u4e2a\u8fdb\u7a0b\u81f3\u5c11\u8981\u5e72\u4e00\u4ef6\u4e8b\uff0c\u6240\u4ee5\uff0c\u4e00\u4e2a\u8fdb\u7a0b\u81f3\u5c11\u6709\u4e00\u4e2a\u7ebf\u7a0b\u3002\u5f53\u7136\uff0c\u50cfWord\u8fd9\u79cd\u590d\u6742\u7684\u8fdb\u7a0b\u53ef\u4ee5\u6709\u591a\u4e2a\u7ebf\u7a0b\uff0c\u591a\u4e2a\u7ebf\u7a0b\u53ef\u4ee5\u540c\u65f6\u6267\u884c\uff0c\u591a\u7ebf\u7a0b\u7684\u6267\u884c\u65b9\u5f0f\u548c\u591a\u8fdb\u7a0b\u662f\u4e00\u6837\u7684\uff0c\u4e5f\u662f\u7531\u64cd\u4f5c\u7cfb\u7edf\u5728\u591a\u4e2a\u7ebf\u7a0b\u4e4b\u95f4\u5feb\u901f\u5207\u6362\uff0c\u8ba9\u6bcf\u4e2a\u7ebf\u7a0b\u90fd\u77ed\u6682\u5730\u4ea4\u66ff\u8fd0\u884c\uff0c\u770b\u8d77\u6765\u5c31\u50cf\u540c\u65f6\u6267\u884c\u4e00\u6837\u3002<\/p>\n<p>\u5f53\u7136\uff0c\u771f\u6b63\u5730\u540c\u65f6\u6267\u884c\u591a\u7ebf\u7a0b\u9700\u8981\u591a\u6838CPU\u624d\u53ef\u80fd\u5b9e\u73b0\u3002<\/p>\n<p>\u666e\u901a\u7684Python\u7a0b\u5e8f\uff0c\u90fd\u662f\u6267\u884c\u5355\u4efb\u52a1\u7684\u8fdb\u7a0b\uff0c\u4e5f\u5c31\u662f\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u3002\u5982\u679c\u6211\u4eec\u8981\u540c\u65f6\u6267\u884c\u591a\u4e2a\u4efb\u52a1\u600e\u4e48\u529e\uff1f<\/p>\n<p>\u6709\u4e24\u79cd\u89e3\u51b3\u65b9\u6848\uff1a<\/p>\n<p>\u4e00\u79cd\u662f\u542f\u52a8\u591a\u4e2a\u8fdb\u7a0b\uff0c\u6bcf\u4e2a\u8fdb\u7a0b\u867d\u7136\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\uff0c\u4f46\u591a\u4e2a\u8fdb\u7a0b\u53ef\u4ee5\u4e00\u5757\u6267\u884c\u591a\u4e2a\u4efb\u52a1\u3002<\/p>\n<p>\u8fd8\u6709\u4e00\u79cd\u65b9\u6cd5\u662f\u542f\u52a8\u4e00\u4e2a\u8fdb\u7a0b\uff0c\u5728\u4e00\u4e2a\u8fdb\u7a0b\u5185\u542f\u52a8\u591a\u4e2a\u7ebf\u7a0b\uff0c\u8fd9\u6837\uff0c\u591a\u4e2a\u7ebf\u7a0b\u4e5f\u53ef\u4ee5\u4e00\u5757\u6267\u884c\u591a\u4e2a\u4efb\u52a1\u3002<\/p>\n<p>\u5f53\u7136\u8fd8\u6709\u7b2c\u4e09\u79cd\u65b9\u6cd5\uff0c\u5c31\u662f\u542f\u52a8\u591a\u4e2a\u8fdb\u7a0b\uff0c\u6bcf\u4e2a\u8fdb\u7a0b\u518d\u542f\u52a8\u591a\u4e2a\u7ebf\u7a0b\uff0c\u8fd9\u6837\u540c\u65f6\u6267\u884c\u7684\u4efb\u52a1\u5c31\u66f4\u591a\u4e86\uff0c\u5f53\u7136\u8fd9\u79cd\u6a21\u578b\u66f4\u590d\u6742\uff0c\u5b9e\u9645\u5f88\u5c11\u91c7\u7528\u3002<\/p>\n<p>\u603b\u7ed3\u4e00\u4e0b\u5c31\u662f\uff0c\u591a\u4efb\u52a1\u7684\u5b9e\u73b0\u67093\u79cd\u65b9\u5f0f\uff1a<\/p>\n<ul>\n<li>\u591a\u8fdb\u7a0b\u6a21\u5f0f\uff1b<\/li>\n<li>\u591a\u7ebf\u7a0b\u6a21\u5f0f\uff1b<\/li>\n<li>\u591a\u8fdb\u7a0b+\u591a\u7ebf\u7a0b\u6a21\u5f0f\u3002<\/li>\n<\/ul>\n<p>\u540c\u65f6\u6267\u884c\u591a\u4e2a\u4efb\u52a1\u901a\u5e38\u5404\u4e2a\u4efb\u52a1\u4e4b\u95f4\u5e76\u4e0d\u662f\u6ca1\u6709\u5173\u8054\u7684\uff0c\u800c\u662f\u9700\u8981\u76f8\u4e92\u901a\u4fe1\u548c\u534f\u8c03\u3002<\/p>\n<p>\u6709\u65f6\uff0c\u4efb\u52a11\u5fc5\u987b\u6682\u505c\u7b49\u5f85\u4efb\u52a12\u5b8c\u6210\u540e\u624d\u80fd\u7ee7\u7eed\u6267\u884c\uff0c\u6709\u65f6\uff0c\u4efb\u52a13\u548c\u4efb\u52a14\u53c8\u4e0d\u80fd\u540c\u65f6\u6267\u884c\uff0c\u6240\u4ee5\uff0c\u591a\u8fdb\u7a0b\u548c\u591a\u7ebf\u7a0b\u7684\u7a0b\u5e8f\u7684\u590d\u6742\u5ea6\u8981\u8fdc\u8fdc\u9ad8\u4e8e\u6211\u4eec\u524d\u9762\u5199\u7684\u5355\u8fdb\u7a0b\u5355\u7ebf\u7a0b\u7684\u7a0b\u5e8f\u3002<\/p>\n<p>\u56e0\u4e3a\u590d\u6742\u5ea6\u9ad8\uff0c\u8c03\u8bd5\u56f0\u96be\uff0c\u6240\u4ee5\uff0c\u4e0d\u662f\u8feb\u4e0d\u5f97\u5df2\uff0c\u6211\u4eec\u4e5f\u4e0d\u60f3\u7f16\u5199\u591a\u4efb\u52a1\u3002\u4f46\u662f\uff0c\u6709\u5f88\u591a\u65f6\u5019\uff0c\u6ca1\u6709\u591a\u4efb\u52a1\u8fd8\u771f\u4e0d\u884c\u3002<\/p>\n<p>\u60f3\u60f3\u5728\u7535\u8111\u4e0a\u770b\u7535\u5f71\uff0c\u5c31\u5fc5\u987b\u7531\u4e00\u4e2a\u7ebf\u7a0b\u64ad\u653e\u89c6\u9891\uff0c\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u64ad\u653e\u97f3\u9891\uff0c\u5426\u5219\uff0c\u5355\u7ebf\u7a0b\u5b9e\u73b0\u7684\u8bdd\u5c31\u53ea\u80fd\u5148\u628a\u89c6\u9891\u64ad\u653e\u5b8c\u518d\u64ad\u653e\u97f3\u9891\uff0c\u6216\u8005\u5148\u628a\u97f3\u9891\u64ad\u653e\u5b8c\u518d\u64ad\u653e\u89c6\u9891\uff0c\u8fd9\u663e\u7136\u662f\u4e0d\u884c\u7684\u3002<\/p>\n<p>Python\u65e2\u652f\u6301\u591a\u8fdb\u7a0b\uff0c\u53c8\u652f\u6301\u591a\u7ebf\u7a0b\uff0c\u6211\u4eec\u4f1a\u8ba8\u8bba\u5982\u4f55\u7f16\u5199\u8fd9\u4e24\u79cd\u591a\u4efb\u52a1\u7a0b\u5e8f\u3002\u7ebf\u7a0b\u662f\u6700\u5c0f\u7684\u6267\u884c\u5355\u5143\uff0c\u800c\u8fdb\u7a0b\u7531\u81f3\u5c11\u4e00\u4e2a\u7ebf\u7a0b\u7ec4\u6210\u3002<\/p>\n<p>\u5982\u4f55\u8c03\u5ea6\u8fdb\u7a0b\u548c\u7ebf\u7a0b\uff0c\u5b8c\u5168\u7531\u64cd\u4f5c\u7cfb\u7edf\u51b3\u5b9a\uff0c\u7a0b\u5e8f\u81ea\u5df1\u4e0d\u80fd\u51b3\u5b9a\u4ec0\u4e48\u65f6\u5019\u6267\u884c\uff0c\u6267\u884c\u591a\u957f\u65f6\u95f4\u3002<\/p>\n<p>\u591a\u8fdb\u7a0b\u548c\u591a\u7ebf\u7a0b\u7684\u7a0b\u5e8f\u6d89\u53ca\u5230\u540c\u6b65\u3001\u6570\u636e\u5171\u4eab\u7684\u95ee\u9898\uff0c\u7f16\u5199\u8d77\u6765\u66f4\u590d\u6742\u3002<\/p>\n<h1>\u00a0<\/h1>\n<h1 style=\"text-align: center;\">\u591a\u8fdb\u7a0b<\/h1>\n<p>\u8981\u8ba9Python\u7a0b\u5e8f\u5b9e\u73b0\u591a\u8fdb\u7a0b\uff08multiprocessing\uff09\uff0c\u6211\u4eec\u5148\u4e86\u89e3\u64cd\u4f5c\u7cfb\u7edf\u7684\u76f8\u5173\u77e5\u8bc6\u3002<\/p>\n<p>Unix\/Linux\u64cd\u4f5c\u7cfb\u7edf\u63d0\u4f9b\u4e86\u4e00\u4e2afork()\u7cfb\u7edf\u8c03\u7528\uff0c\u5b83\u975e\u5e38\u7279\u6b8a\u3002\u666e\u901a\u7684\u51fd\u6570\u8c03\u7528\uff0c\u8c03\u7528\u4e00\u6b21\uff0c\u8fd4\u56de\u4e00\u6b21\uff0c\u4f46\u662ffork()\u8c03\u7528\u4e00\u6b21\uff0c\u8fd4\u56de\u4e24\u6b21\uff0c\u56e0\u4e3a\u64cd\u4f5c\u7cfb\u7edf\u81ea\u52a8\u628a\u5f53\u524d\u8fdb\u7a0b\uff08\u79f0\u4e3a\u7236\u8fdb\u7a0b\uff09\u590d\u5236\u4e86\u4e00\u4efd\uff08\u79f0\u4e3a\u5b50\u8fdb\u7a0b\uff09\uff0c\u7136\u540e\uff0c\u5206\u522b\u5728\u7236\u8fdb\u7a0b\u548c\u5b50\u8fdb\u7a0b\u5185\u8fd4\u56de\u3002<\/p>\n<p>\u5b50\u8fdb\u7a0b\u6c38\u8fdc\u8fd4\u56de0\uff0c\u800c\u7236\u8fdb\u7a0b\u8fd4\u56de\u5b50\u8fdb\u7a0b\u7684ID\u3002\u8fd9\u6837\u505a\u7684\u7406\u7531\u662f\uff0c\u4e00\u4e2a\u7236\u8fdb\u7a0b\u53ef\u4ee5fork\u51fa\u5f88\u591a\u5b50\u8fdb\u7a0b\uff0c\u6240\u4ee5\uff0c\u7236\u8fdb\u7a0b\u8981\u8bb0\u4e0b\u6bcf\u4e2a\u5b50\u8fdb\u7a0b\u7684ID\uff0c\u800c\u5b50\u8fdb\u7a0b\u53ea\u9700\u8981\u8c03\u7528getppid()\u5c31\u53ef\u4ee5\u62ff\u5230\u7236\u8fdb\u7a0b\u7684ID\u3002<\/p>\n<p>Python\u7684os\u6a21\u5757\u5c01\u88c5\u4e86\u5e38\u89c1\u7684\u7cfb\u7edf\u8c03\u7528\uff0c\u5176\u4e2d\u5c31\u5305\u62ecfork\uff0c\u53ef\u4ee5\u5728Python\u7a0b\u5e8f\u4e2d\u8f7b\u677e\u521b\u5efa\u5b50\u8fdb\u7a0b\uff1a<\/p>\n\n\n\n<pre>\n<span>import os<\/span>\n<span><\/span>\n<span>print(&#x27;Process (%s) start...&#x27; % os.getpid())<\/span>\n<span># Only works on Unix\/Linux\/Mac:<\/span>\n<span>pid = os.fork()<\/span>\n<span>if pid == 0:<\/span>\n<span>    print(&#x27;I am child process (%s) and my parent is %s.&#x27; % (os.getpid(), os.getppid()))<\/span>\n<span>else:<\/span>\n<span>    print(&#x27;I (%s) just created a child process (%s).&#x27; % (os.getpid(), pid))<\/span>\n<\/pre>\n\n\n\n<p>\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre>\n<span>Process (876) start...<\/span>\n<span>I (876) just created a child process (877).<\/span>\n<span>I am child process (877) and my parent is 876. <\/span>\n<\/pre>\n\n\n\n<p class=\"wp-block-preformatted\">\u7531\u4e8eWindows\u6ca1\u6709fork\u8c03\u7528\uff0c\u4e0a\u9762\u7684\u4ee3\u7801\u5728Windows\u4e0a\u65e0\u6cd5\u8fd0\u884c\u3002\u800cMac\u7cfb\u7edf\u662f\u57fa\u4e8eBSD\uff08Unix\u7684\u4e00\u79cd\uff09\u5185\u6838\uff0c\u6240\u4ee5\uff0c\u5728Mac\u4e0b\u8fd0\u884c\u662f\u6ca1\u6709\u95ee\u9898\u7684\uff0c\u63a8\u8350\u5927\u5bb6\u7528Mac\u5b66Python\uff01<\/p>\n<p class=\"wp-block-preformatted\">\u6709\u4e86fork\u8c03\u7528\uff0c\u4e00\u4e2a\u8fdb\u7a0b\u5728\u63a5\u5230\u65b0\u4efb\u52a1\u65f6\u5c31\u53ef\u4ee5\u590d\u5236\u51fa\u4e00\u4e2a\u5b50\u8fdb\u7a0b\u6765\u5904\u7406\u65b0\u4efb\u52a1\uff0c\u5e38\u89c1\u7684Apache\u670d\u52a1\u5668\u5c31\u662f\u7531\u7236\u8fdb\u7a0b\u76d1\u542c\u7aef\u53e3\uff0c\u6bcf\u5f53\u6709\u65b0\u7684http\u8bf7\u6c42\u65f6\uff0c\u5c31fork\u51fa\u5b50\u8fdb\u7a0b\u6765\u5904\u7406\u65b0\u7684http\u8bf7\u6c42\u3002<\/p>\n<h3>\u00a0<\/h3>\n<h3 class=\"wp-block-preformatted\"><strong>multiprocessing<\/strong><\/h3>\n<p class=\"wp-block-preformatted\">\u5982\u679c\u4f60\u6253\u7b97\u7f16\u5199\u591a\u8fdb\u7a0b\u7684\u670d\u52a1\u7a0b\u5e8f\uff0cUnix\/Linux\u65e0\u7591\u662f\u6b63\u786e\u7684\u9009\u62e9\u3002\u7531\u4e8eWindows\u6ca1\u6709fork\u8c03\u7528\uff0c\u96be\u9053\u5728Windows\u4e0a\u65e0\u6cd5\u7528Python\u7f16\u5199\u591a\u8fdb\u7a0b\u7684\u7a0b\u5e8f\uff1f<br data-rich-text-line-break=\"true\" \/>\u7531\u4e8ePython\u662f\u8de8\u5e73\u53f0\u7684\uff0c\u81ea\u7136\u4e5f\u5e94\u8be5\u63d0\u4f9b\u4e00\u4e2a\u8de8\u5e73\u53f0\u7684\u591a\u8fdb\u7a0b\u652f\u6301\u3002<\/p>\n<p class=\"wp-block-preformatted\">multiprocessing\u6a21\u5757\u5c31\u662f\u8de8\u5e73\u53f0\u7248\u672c\u7684\u591a\u8fdb\u7a0b\u6a21\u5757\u3002<\/p>\n<p class=\"wp-block-preformatted\">multiprocessing\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e2aProcess\u7c7b\u6765\u4ee3\u8868\u4e00\u4e2a\u8fdb\u7a0b\u5bf9\u8c61\uff0c\u4e0b\u9762\u7684\u4f8b\u5b50\u6f14\u793a\u4e86\u542f\u52a8\u4e00\u4e2a\u5b50\u8fdb\u7a0b\u5e76\u7b49\u5f85\u5176\u7ed3\u675f\uff1a<\/p>\n\n\n\n<pre>\n<span>from multiprocessing import Process<\/span>\n<span>import os<\/span>\n<span><\/span>\n<span>#\u5b50\u8fdb\u7a0b\u8981\u6267\u884c\u7684\u4ee3\u7801<\/span>\n<span>def run_proc(name):<\/span>\n<span>    print(&#x27;Run child process %s (%s)\u2026&#x27; % (name, os.getpid()))<\/span>\n<span><\/span>\n<span>if __name__==&#x27;__main__&#x27;:<\/span>\n<span>    print(&#x27;Parent process %s.&#x27; % os.getpid())<\/span>\n<span>    p = Process(target=run_proc, args=(&#x27;test&#x27;,))<\/span>\n<span>    print(&#x27;Child process will start.&#x27;)<\/span>\n<span>    p.start()<\/span>\n<span>    p.join()<\/span>\n<span>    print(&#x27;Child process end.&#x27;)<\/span>\n<\/pre>\n\n\n\n<p>\u6267\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre>\n<span>Parent process 928.<\/span>\n<span>Child process will start.<\/span>\n<span>Run child process test (929)\u2026<\/span>\n<span>Process end.<\/span>\n<\/pre>\n\n\n\n<p>\u521b\u5efa\u5b50\u8fdb\u7a0b\u65f6\uff0c\u53ea\u9700\u8981\u4f20\u5165\u4e00\u4e2a\u6267\u884c\u51fd\u6570\u548c\u51fd\u6570\u7684\u53c2\u6570\uff0c\u521b\u5efa\u4e00\u4e2aProcess\u5b9e\u4f8b\uff0c\u7528start()\u65b9\u6cd5\u542f\u52a8\uff0c\u8fd9\u6837\u521b\u5efa\u8fdb\u7a0b\u6bd4fork()\u8fd8\u8981\u7b80\u5355\u3002<\/p>\n<p>join()\u65b9\u6cd5\u53ef\u4ee5\u7b49\u5f85\u5b50\u8fdb\u7a0b\u7ed3\u675f\u540e\u518d\u7ee7\u7eed\u5f80\u4e0b\u8fd0\u884c\uff0c\u901a\u5e38\u7528\u4e8e\u8fdb\u7a0b\u95f4\u7684\u540c\u6b65\u3002<\/p>\n<h3>\u00a0<\/h3>\n<h3>Pool<\/h3>\n<p>\u5982\u679c\u8981\u542f\u52a8\u5927\u91cf\u7684\u5b50\u8fdb\u7a0b\uff0c\u53ef\u4ee5\u7528\u8fdb\u7a0b\u6c60\u7684\u65b9\u5f0f\u6279\u91cf\u521b\u5efa\u5b50\u8fdb\u7a0b\uff1a<\/p>\n\n\n\n<pre>\n<span>from multiprocessing import Pool<\/span>\n<span>import os, time, random<\/span>\n<span><\/span>\n<span>def long_time_task(name):<\/span>\n<span>    print(&#x27;Run task %s (%s)\u2026&#x27; % (name, os.getpid())<\/span>\n<span>    start = time.time()<\/span>\n<span>    time.sleep(random.random() * 3)<\/span>\n<span>    end = time.time()<\/span>\n<span>    print(&#x27;Task %s runs %0.2f seconds.&#x27; % (name, (end - start)))<\/span>\n<span><\/span>\n<span>if __name__==&#x27;__main__&#x27;:<\/span>\n<span>    print(&#x27;Parent process %s.&#x27; % os.getpid())<\/span>\n<span>    p = Pool(4)<\/span>\n<span>    for i in range(5):<\/span>\n<span>        p.apply_async(long_time_task, args=(i,))<\/span>\n<span>    print(&#x27;Waiting for all subprocesses done\u2026&#x27;)<\/span>\n<span>    p.close()<\/span>\n<span>    p.join()<\/span>\n<span>    print(&#x27;All subprocesses done.&#x27;)<\/span>\n<\/pre>\n\n\n\n<p> \u6267\u884c\u7ed3\u679c\u5982\u4e0b\uff1a <\/p>\n\n\n\n<pre>\n<span>Parent process 669.<\/span>\n<span>Waiting for all subprocesses done...<\/span>\n<span>Run task 0 (671)...<\/span>\n<span>Run task 1 (672)...<\/span>\n<span>Run task 2 (673)...<\/span>\n<span>Run task 3 (674)...<\/span>\n<span>Task 2 runs 0.14 seconds.<\/span>\n<span>Run task 4 (673)...<\/span>\n<span>Task 1 runs 0.27 seconds.<\/span>\n<span>Task 3 runs 0.86 seconds.<\/span>\n<span>Task 0 runs 1.41 seconds.<\/span>\n<span>Task 4 runs 1.91 seconds.<\/span>\n<span>All subprocesses done.<\/span>\n<\/pre>\n\n\n\n<p>\u4ee3\u7801\u89e3\u8bfb\uff1a<\/p>\n<p>\u5bf9Pool\u5bf9\u8c61\u8c03\u7528join()\u65b9\u6cd5\u4f1a\u7b49\u5f85\u6240\u6709\u5b50\u8fdb\u7a0b\u6267\u884c\u5b8c\u6bd5\uff0c\u8c03\u7528join()\u4e4b\u524d\u5fc5\u987b\u5148\u8c03\u7528close()\uff0c\u8c03\u7528close()\u4e4b\u540e\u5c31\u4e0d\u80fd\u7ee7\u7eed\u6dfb\u52a0\u65b0\u7684Process\u4e86\u3002<\/p>\n<p>\u8bf7\u6ce8\u610f\u8f93\u51fa\u7684\u7ed3\u679c\uff0ctask 0\uff0c1\uff0c2\uff0c3\u662f\u7acb\u523b\u6267\u884c\u7684\uff0c\u800ctask 4\u8981\u7b49\u5f85\u524d\u9762\u67d0\u4e2atask\u5b8c\u6210\u540e\u624d\u6267\u884c\uff0c\u8fd9\u662f\u56e0\u4e3aPool\u7684\u9ed8\u8ba4\u5927\u5c0f\u5728\u6211\u7684\u7535\u8111\u4e0a\u662f4\uff0c\u56e0\u6b64\uff0c\u6700\u591a\u540c\u65f6\u6267\u884c4\u4e2a\u8fdb\u7a0b\u3002\u8fd9\u662fPool\u6709\u610f\u8bbe\u8ba1\u7684\u9650\u5236\uff0c\u5e76\u4e0d\u662f\u64cd\u4f5c\u7cfb\u7edf\u7684\u9650\u5236\u3002<\/p>\n<p>\u5982\u679c\u6539\u6210<code>p = Pool(5)<\/code>\uff0c\u5c31\u53ef\u4ee5\u540c\u65f6\u8dd15\u4e2a\u8fdb\u7a0b\u3002<\/p>\n<p>\u7531\u4e8ePool\u7684\u9ed8\u8ba4\u5927\u5c0f\u662fCPU\u7684\u6838\u6570\uff0c\u5982\u679c\u4f60\u4e0d\u5e78\u62e5\u67098\u6838CPU\uff0c\u4f60\u8981\u63d0\u4ea4\u81f3\u5c119\u4e2a\u5b50\u8fdb\u7a0b\u624d\u80fd\u770b\u5230\u4e0a\u9762\u7684\u7b49\u5f85\u6548\u679c\u3002<\/p>\n<h3>\u00a0<\/h3>\n<h3>\u5b50\u8fdb\u7a0b<\/h3>\n<p>\u5f88\u591a\u65f6\u5019\uff0c\u5b50\u8fdb\u7a0b\u5e76\u4e0d\u662f\u81ea\u8eab\uff0c\u800c\u662f\u4e00\u4e2a\u5916\u90e8\u8fdb\u7a0b\u3002\u6211\u4eec\u521b\u5efa\u4e86\u5b50\u8fdb\u7a0b\u540e\uff0c\u8fd8\u9700\u8981\u63a7\u5236\u5b50\u8fdb\u7a0b\u7684\u8f93\u5165\u548c\u8f93\u51fa\u3002<\/p>\n<p>subprocess\u6a21\u5757\u53ef\u4ee5\u8ba9\u6211\u4eec\u975e\u5e38\u65b9\u4fbf\u5730\u542f\u52a8\u4e00\u4e2a\u5b50\u8fdb\u7a0b\uff0c\u7136\u540e\u63a7\u5236\u5176\u8f93\u5165\u548c\u8f93\u51fa\u3002<\/p>\n<p>\u4e0b\u9762\u7684\u4f8b\u5b50\u6f14\u793a\u4e86\u5982\u4f55\u5728Python\u4ee3\u7801\u4e2d\u8fd0\u884c\u547d\u4ee4nslookup www.python.org\uff0c\u8fd9\u548c\u547d\u4ee4\u884c\u76f4\u63a5\u8fd0\u884c\u7684\u6548\u679c\u662f\u4e00\u6837\u7684\uff1a<\/p>\n\n\n\n<pre>\n<span>import subprocess<\/span>\n<span><\/span>\n<span>print(&#x27;$ nslookup www.python.org&#x27;)<\/span>\n<span>r = subprocess.call([&#x27;nslookup&#x27;, &#x27;www.python.org&#x27;])<\/span>\n<span>print(&#x27;Exit code:&#x27;, r)<\/span>\n<\/pre>\n\n\n\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre>\n<span>$ nslookup www.python.org<\/span>\n<span>Server: 192.168.19.4<\/span>\n<span>Address: 192.168.19.4#53<\/span>\n<span>Non-authoritative answer:<\/span>\n<span>www.python.org canonical name = python.map.fastly.net.<\/span>\n<span>Name: python.map.fastly.net<\/span>\n<span>Address: 199.27.79.223<\/span>\n<span>Exit code: 0<\/span>\n<\/pre>\n\n\n\n<p>\u5982\u679c\u5b50\u8fdb\u7a0b\u8fd8\u9700\u8981\u8f93\u5165\uff0c\u5219\u53ef\u4ee5\u901a\u8fc7communicate()\u65b9\u6cd5\u8f93\u5165\uff1a<\/p>\n\n\n\n<pre>\n<span>import subprocess<\/span>\n<span><\/span>\n<span>print(&#x27;$ nslookup&#x27;)<\/span>\n<span>p = subprocess.Popen([&#x27;nslookup&#x27;], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)<\/span>\n<span>output, err = p.communicate(b&#x27;set q=mx\\npython.org\\nexit\\n&#x27;\uff09<\/span>\n<span>print(output.decode(&#x27;utf-8&#x27;))<\/span>\n<span>print(&#x27;Exit code:&#x27;, p.returncode)<\/span>\n<\/pre>\n\n\n\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u76f8\u5f53\u4e8e\u5728\u547d\u4ee4\u884c\u6267\u884c\u547d\u4ee4nslookup\uff0c\u7136\u540e\u624b\u52a8\u8f93\u5165\uff1a<\/p>\n\n\n\n<pre>\n<span>set q=mx<\/span>\n<span>python.org<\/span>\n<span>exit<\/span>\n<\/pre>\n\n\n\n<p>\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre>\n<span>$ nslookup<\/span>\n<span>Server: 192.168.19.4<\/span>\n<span>Address: 192.168.19.4#53<\/span>\n<span>Non-authoritative answer:<\/span>\n<span>python.org mail exchanger = 50 mail.python.org.<\/span>\n<span>Authoritative answers can be found from:<\/span>\n<span>mail.python.org internet address = 82.94.164.166<\/span>\n<span>mail.python.org has AAAA address 2001:888:2000:d::a6<\/span>\n<span>Exit code: 0<\/span>\n<\/pre>\n\n\n\n<h3>\u00a0<\/h3>\n<h3>\u8fdb\u7a0b\u95f4\u901a\u4fe1<\/h3>\n<p>Process\u4e4b\u95f4\u80af\u5b9a\u662f\u9700\u8981\u901a\u4fe1\u7684\uff0c\u64cd\u4f5c\u7cfb\u7edf\u63d0\u4f9b\u4e86\u5f88\u591a\u673a\u5236\u6765\u5b9e\u73b0\u8fdb\u7a0b\u95f4\u7684\u901a\u4fe1\u3002Python\u7684multiprocessing\u6a21\u5757\u5305\u88c5\u4e86\u5e95\u5c42\u7684\u673a\u5236\uff0c\u63d0\u4f9b\u4e86Queue\u3001Pipes\u7b49\u591a\u79cd\u65b9\u5f0f\u6765\u4ea4\u6362\u6570\u636e\u3002<\/p>\n<p>\u6211\u4eec\u4ee5Queue\u4e3a\u4f8b\uff0c\u5728\u7236\u8fdb\u7a0b\u4e2d\u521b\u5efa\u4e24\u4e2a\u5b50\u8fdb\u7a0b\uff0c\u4e00\u4e2a\u5f80Queue\u91cc\u5199\u6570\u636e\uff0c\u4e00\u4e2a\u4eceQueue\u91cc\u8bfb\u6570\u636e\uff1a<\/p>\n\n\n\n<pre>\n<span>from multiprocessing import Process, Queue<\/span>\n<span>import os, time, random<\/span>\n<span><\/span>\n<span>#\u5199\u6570\u636e\u8fdb\u7a0b\u6267\u884c\u7684\u4ee3\u7801:<\/span>\n<span>def write(q):<\/span>\n<span>    print(&#x27;Process to write: %s&#x27; % os.getpid())<\/span>\n<span>    for value in [&#x27;A&#x27;, &#x27;B&#x27;, &#x27;C&#x27;]:<\/span>\n<span>        print(&#x27;Put %s to queue\u2026&#x27; % value)<\/span>\n<span>        q.put(value)<\/span>\n<span>        time.sleep(random.random())<\/span>\n<span><\/span>\n<span>#\u8bfb\u6570\u636e\u8fdb\u7a0b\u6267\u884c\u7684\u4ee3\u7801:<\/span>\n<span>def read(q):<\/span>\n<span>    print(&#x27;Process to read: %s&#x27; % os.getpid())<\/span>\n<span>    while True:<\/span>\n<span>        value = q.get(True)<\/span>\n<span>        print(&#x27;Get %s from queue.&#x27; % value)<\/span>\n<span><\/span>\n<span>if __name__==&#x27;__main__&#x27;:<\/span>\n<span>    #\u7236\u8fdb\u7a0b\u521b\u5efaQueue\uff0c\u5e76\u4f20\u7ed9\u5404\u4e2a\u5b50\u8fdb\u7a0b\uff1a<\/span>\n<span>    q = Queue()<\/span>\n<span>    pw = Process(target=write, args=(q,))<\/span>\n<span>    pr = Process(target=read, args=(q,))<\/span>\n<span>    #\u542f\u52a8\u5b50\u8fdb\u7a0bpw\uff0c\u5199\u5165:<\/span>\n<span>    pw.start()<\/span>\n<span>    #\u542f\u52a8\u5b50\u8fdb\u7a0bpr\uff0c\u8bfb\u53d6:<\/span>\n<span>    pr.start()<\/span>\n<span>    #\u7b49\u5f85pw\u7ed3\u675f:<\/span>\n<span>    pw.join()<\/span>\n<span>    #pr\u8fdb\u7a0b\u91cc\u662f\u6b7b\u5faa\u73af\uff0c\u65e0\u6cd5\u7b49\u5f85\u5176\u7ed3\u675f\uff0c\u53ea\u80fd\u5f3a\u884c\u7ec8\u6b62:<\/span>\n<span>    pr.terminate()<\/span>\n<\/pre>\n\n\n\n<p>\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre>\n<span>Process to write: 50563<\/span>\n<span>Put A to queue\u2026<\/span>\n<span>Process to read: 50564<\/span>\n<span>Get A from queue.<\/span>\n<span>Put B to queue\u2026<\/span>\n<span>Get B from queue.<\/span>\n<span>Put C to queue\u2026<\/span>\n<span>Get C from queue.<\/span>\n<\/pre>\n\n\n\n<p>\u5728Unix\/Linux\u4e0b\uff0cmultiprocessing\u6a21\u5757\u5c01\u88c5\u4e86fork()\u8c03\u7528\uff0c\u4f7f\u6211\u4eec\u4e0d\u9700\u8981\u5173\u6ce8fork()\u7684\u7ec6\u8282\u3002<\/p>\n<p>\u7531\u4e8eWindows\u6ca1\u6709fork\u8c03\u7528\uff0c\u56e0\u6b64\uff0cmultiprocessing\u9700\u8981\u201c\u6a21\u62df\u201d\u51fafork\u7684\u6548\u679c\uff0c\u7236\u8fdb\u7a0b\u6240\u6709Python\u5bf9\u8c61\u90fd\u5fc5\u987b\u901a\u8fc7pickle\u5e8f\u5217\u5316\u518d\u4f20\u5230\u5b50\u8fdb\u7a0b\u53bb\u3002<\/p>\n<p>\u6240\u4ee5\uff0c\u5982\u679cmultiprocessing\u5728Windows\u4e0b\u8c03\u7528\u5931\u8d25\u4e86\uff0c\u8981\u5148\u8003\u8651\u662f\u4e0d\u662fpickle\u5931\u8d25\u4e86\u3002<\/p>\n<p>\u5728Unix\/Linux\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528fork()\u8c03\u7528\u5b9e\u73b0\u591a\u8fdb\u7a0b\u3002<\/p>\n<p>\u8981\u5b9e\u73b0\u8de8\u5e73\u53f0\u7684\u591a\u8fdb\u7a0b\uff0c\u53ef\u4ee5\u4f7f\u7528<code>multiprocessing<\/code>\u6a21\u5757\u3002<\/p>\n<p>\u8fdb\u7a0b\u95f4\u901a\u4fe1\u662f\u901a\u8fc7Queue\u3001Pipes\u7b49\u5b9e\u73b0\u7684\u3002<\/p>\n<h1>\u00a0<\/h1>\n<h1 style=\"text-align: center;\">\u591a\u7ebf\u7a0b<\/h1>\n<p>\u591a\u4efb\u52a1\u53ef\u4ee5\u7531\u591a\u8fdb\u7a0b\u5b8c\u6210\uff0c\u4e5f\u53ef\u4ee5\u7531\u4e00\u4e2a\u8fdb\u7a0b\u5185\u7684\u591a\u7ebf\u7a0b\u5b8c\u6210\u3002<\/p>\n<p>\u6211\u4eec\u524d\u9762\u63d0\u5230\u4e86\u8fdb\u7a0b\u662f\u7531\u82e5\u5e72\u7ebf\u7a0b\u7ec4\u6210\u7684\uff0c\u4e00\u4e2a\u8fdb\u7a0b\u81f3\u5c11\u6709\u4e00\u4e2a\u7ebf\u7a0b\u3002<\/p>\n<p>\u7531\u4e8e\u7ebf\u7a0b\u662f\u64cd\u4f5c\u7cfb\u7edf\u76f4\u63a5\u652f\u6301\u7684\u6267\u884c\u5355\u5143\uff0c\u56e0\u6b64\uff0c\u9ad8\u7ea7\u8bed\u8a00\u901a\u5e38\u90fd\u5185\u7f6e\u591a\u7ebf\u7a0b\u7684\u652f\u6301\uff0cPython\u4e5f\u4e0d\u4f8b\u5916\uff0c\u5e76\u4e14\uff0cPython\u7684\u7ebf\u7a0b\u662f\u771f\u6b63\u7684Posix Thread\uff0c\u800c\u4e0d\u662f\u6a21\u62df\u51fa\u6765\u7684\u7ebf\u7a0b\u3002<\/p>\n<p>Python\u7684\u6807\u51c6\u5e93\u63d0\u4f9b\u4e86\u4e24\u4e2a\u6a21\u5757\uff1a<code>_thread<\/code>\u548c<code>threading<\/code>\uff0c_thread\u662f\u4f4e\u7ea7\u6a21\u5757\uff0cthreading\u662f\u9ad8\u7ea7\u6a21\u5757\uff0c\u5bf9_thread\u8fdb\u884c\u4e86\u5c01\u88c5\u3002\u7edd\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ea\u9700\u8981\u4f7f\u7528threading\u8fd9\u4e2a\u9ad8\u7ea7\u6a21\u5757\u3002<\/p>\n<p>\u542f\u52a8\u4e00\u4e2a\u7ebf\u7a0b\u5c31\u662f\u628a\u4e00\u4e2a\u51fd\u6570\u4f20\u5165\u5e76\u521b\u5efaThread\u5b9e\u4f8b\uff0c\u7136\u540e\u8c03\u7528start()\u5f00\u59cb\u6267\u884c\uff1a<\/p>\n\n\n\n<pre>\n<span>import time, threading<\/span>\n<span><\/span>\n<span>#\u65b0\u7ebf\u7a0b\u6267\u884c\u7684\u4ee3\u7801:<\/span>\n<span>def loop():<\/span>\n<span>    print(&#x27;thread %s is running\u2026&#x27; % threading.current_thread().name)<\/span>\n<span>    n = 0<\/span>\n<span>    while n &lt; 5:<\/span>\n<span>        n = n + 1 <\/span>\n<span>        print(&#x27;thread %s &gt;&gt;&gt; %s&#x27; % (threading.current_thread().name, n))<\/span>\n<span>        time.sleep(1)<\/span>\n<span>    print(&#x27;thread %s ended.&#x27; % threading.current_thread().name)<\/span>\n<span><\/span>\n<span>print(&#x27;thread %s is running\u2026&#x27; % threading.current_thread().name)<\/span>\n<span>t = threading.Thread(target=loop, name=&#x27;LoopThread&#x27;)<\/span>\n<span>t.start()<\/span>\n<span>t.join()<\/span>\n<span>print(&#x27;thread %s ended.&#x27; % threading.current_thread().name)<\/span>\n<\/pre>\n\n\n\n<p>\u6267\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre>\n<span>thread MainThread is running\u2026<\/span>\n<span>thread LoopThread is running\u2026<\/span>\n<span>thread LoopThread &gt;&gt;&gt; 1<\/span>\n<span>thread LoopThread &gt;&gt;&gt; 2<\/span>\n<span>thread LoopThread &gt;&gt;&gt; 3<\/span>\n<span>thread LoopThread &gt;&gt;&gt; 4<\/span>\n<span>thread LoopThread &gt;&gt;&gt; 5<\/span>\n<span>thread LoopThread ended.<\/span>\n<span>thread MainThread ended.<\/span>\n<\/pre>\n\n\n\n<p>\u7531\u4e8e\u4efb\u4f55\u8fdb\u7a0b\u9ed8\u8ba4\u5c31\u4f1a\u542f\u52a8\u4e00\u4e2a\u7ebf\u7a0b\uff0c\u6211\u4eec\u628a\u8be5\u7ebf\u7a0b\u79f0\u4e3a\u4e3b\u7ebf\u7a0b\uff0c\u4e3b\u7ebf\u7a0b\u53c8\u53ef\u4ee5\u542f\u52a8\u65b0\u7684\u7ebf\u7a0b\uff0cPython\u7684threading\u6a21\u5757\u6709\u4e2acurrent_thread()\u51fd\u6570\uff0c\u5b83\u6c38\u8fdc\u8fd4\u56de\u5f53\u524d\u7ebf\u7a0b\u7684\u5b9e\u4f8b\u3002<\/p>\n<p>\u4e3b\u7ebf\u7a0b\u5b9e\u4f8b\u7684\u540d\u5b57\u53ebMainThread\uff0c\u5b50\u7ebf\u7a0b\u7684\u540d\u5b57\u5728\u521b\u5efa\u65f6\u6307\u5b9a\uff0c\u6211\u4eec\u7528LoopThread\u547d\u540d\u5b50\u7ebf\u7a0b\u3002\u540d\u5b57\u4ec5\u4ec5\u5728\u6253\u5370\u65f6\u7528\u6765\u663e\u793a\uff0c\u5b8c\u5168\u6ca1\u6709\u5176\u4ed6\u610f\u4e49\uff0c\u5982\u679c\u4e0d\u8d77\u540d\u5b57Python\u5c31\u81ea\u52a8\u7ed9\u7ebf\u7a0b\u547d\u540d\u4e3aThread-1\uff0cThread-2\u2026\u2026<\/p>\n<h3>\u00a0<\/h3>\n<h3>Lock<\/h3>\n<p>\u591a\u7ebf\u7a0b\u548c\u591a\u8fdb\u7a0b\u6700\u5927\u7684\u4e0d\u540c\u5728\u4e8e\uff0c\u591a\u8fdb\u7a0b\u4e2d\uff0c\u540c\u4e00\u4e2a\u53d8\u91cf\uff0c\u5404\u81ea\u6709\u4e00\u4efd\u62f7\u8d1d\u5b58\u5728\u4e8e\u6bcf\u4e2a\u8fdb\u7a0b\u4e2d\uff0c\u4e92\u4e0d\u5f71\u54cd\uff0c\u800c\u591a\u7ebf\u7a0b\u4e2d\uff0c\u6240\u6709\u53d8\u91cf\u90fd\u7531\u6240\u6709\u7ebf\u7a0b\u5171\u4eab\u3002<\/p>\n<p>\u6240\u4ee5\uff0c\u4efb\u4f55\u4e00\u4e2a\u53d8\u91cf\u90fd\u53ef\u4ee5\u88ab\u4efb\u4f55\u4e00\u4e2a\u7ebf\u7a0b\u4fee\u6539\uff0c\u56e0\u6b64\uff0c\u7ebf\u7a0b\u4e4b\u95f4\u5171\u4eab\u6570\u636e\u6700\u5927\u7684\u5371\u9669\u5728\u4e8e\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u6539\u4e00\u4e2a\u53d8\u91cf\uff0c\u628a\u5185\u5bb9\u7ed9\u6539\u4e71\u4e86\u3002<\/p>\n<p>\u6765\u770b\u770b\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u64cd\u4f5c\u4e00\u4e2a\u53d8\u91cf\u600e\u4e48\u628a\u5185\u5bb9\u7ed9\u6539\u4e71\u4e86\uff1a<\/p>\n\n\n\n<pre>\n<span>import time, threading<\/span>\n<span><\/span>\n<span># \u5047\u5b9a\u8fd9\u662f\u4f60\u7684\u94f6\u884c\u5b58\u6b3e:<\/span>\n<span>balance = 0<\/span>\n<span><\/span>\n<span>def change_it(n):<\/span>\n<span>    # \u5148\u5b58\u540e\u53d6\uff0c\u7ed3\u679c\u5e94\u8be5\u4e3a0:<\/span>\n<span>    global balance<\/span>\n<span>    balance = balance + n<\/span>\n<span>    balance = balance - n<\/span>\n<span><\/span>\n<span>def run_thread(n):<\/span>\n<span>    for i in range(100000):<\/span>\n<span>        change_it(n)<\/span>\n<span><\/span>\n<span>t1 = threading.Thread(target=run_thread, args=(5,))<\/span>\n<span>t2 = threading.Thread(target=run_thread, args=(8,))<\/span>\n<span>t1.start()<\/span>\n<span>t2.start()<\/span>\n<span>t1.join()<\/span>\n<span>t2.join()<\/span>\n<span>print(balance) <\/span>\n<\/pre>\n\n\n\n<p class=\"wp-block-preformatted\">\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5171\u4eab\u53d8\u91cfbalance\uff0c\u521d\u59cb\u503c\u4e3a0\uff0c\u5e76\u4e14\u542f\u52a8\u4e24\u4e2a\u7ebf\u7a0b\uff0c\u5148\u5b58\u540e\u53d6\uff0c\u7406\u8bba\u4e0a\u7ed3\u679c\u5e94\u8be5\u4e3a0\u3002<\/p>\n<p class=\"wp-block-preformatted\">\u4f46\u662f\uff0c\u7531\u4e8e\u7ebf\u7a0b\u7684\u8c03\u5ea6\u662f\u7531\u64cd\u4f5c\u7cfb\u7edf\u51b3\u5b9a\u7684\uff0c\u5f53t1\u3001t2\u4ea4\u66ff\u6267\u884c\u65f6\uff0c\u53ea\u8981\u5faa\u73af\u6b21\u6570\u8db3\u591f\u591a\uff0cbalance\u7684\u7ed3\u679c\u5c31\u4e0d\u4e00\u5b9a\u662f0\u4e86\u3002<br \/><br data-rich-text-line-break=\"true\" \/>\u539f\u56e0\u662f\u56e0\u4e3a\u9ad8\u7ea7\u8bed\u8a00\u7684\u4e00\u6761\u8bed\u53e5\u5728CPU\u6267\u884c\u65f6\u662f\u82e5\u5e72\u6761\u8bed\u53e5\uff0c\u5373\u4f7f\u4e00\u4e2a\u7b80\u5355\u7684\u8ba1\u7b97\uff1a<\/p>\n\n\n\n<pre>\n<span>balance = balance + n <\/span>\n<\/pre>\n\n\n\n<p>\u4e5f\u5206\u4e24\u6b65\uff1a<\/p>\n<p>\u8ba1\u7b97balance + n\uff0c\u5b58\u5165\u4e34\u65f6\u53d8\u91cf\u4e2d\uff1b<\/p>\n<p>\u5c06\u4e34\u65f6\u53d8\u91cf\u7684\u503c\u8d4b\u7ed9balance\u3002<\/p>\n<p>\u4e5f\u5c31\u662f\u53ef\u4ee5\u770b\u6210\uff1a<\/p>\n\n\n\n<pre>\n<span>x = balance + n<\/span>\n<span>balance = x <\/span>\n<\/pre>\n\n\n\n<p class=\"wp-block-preformatted\">\u7531\u4e8ex\u662f\u5c40\u90e8\u53d8\u91cf\uff0c\u4e24\u4e2a\u7ebf\u7a0b\u5404\u81ea\u90fd\u6709\u81ea\u5df1\u7684x\uff0c\u5f53\u4ee3\u7801\u6b63\u5e38\u6267\u884c\u65f6\uff1a<\/p>\n\n\n\n<pre>\n<span>\u521d\u59cb\u503c balance = 0<\/span>\n<span><\/span>\n<span>t1: x1 = balance + 5 # x1 = 0 + 5 = 5<\/span>\n<span>t1: balance = x1 # balance = 5<\/span>\n<span>t1: x1 = balance - 5 # x1 = 5 - 5 = 0<\/span>\n<span>t1: balance = x1 # balance = 0<\/span>\n<span><\/span>\n<span>t2: x2 = balance + 8 # x2 = 0 + 8 = 8<\/span>\n<span>t2: balance = x2 # balance = 8<\/span>\n<span>t2: x2 = balance - 8 # x2 = 8 - 8 = 0<\/span>\n<span>t2: balance = x2 # balance = 0<\/span>\n<span><\/span>\n<span>\u7ed3\u679c balance = 0 <\/span>\n<\/pre>\n\n\n\n<p>\u4f46\u662ft1\u548ct2\u662f\u4ea4\u66ff\u8fd0\u884c\u7684\uff0c\u5982\u679c\u64cd\u4f5c\u7cfb\u7edf\u4ee5\u4e0b\u9762\u7684\u987a\u5e8f\u6267\u884ct1\u3001t2\uff1a<\/p>\n\n\n\n<pre>\n<span>\u521d\u59cb\u503c balance = 0<\/span>\n<span><\/span>\n<span>t1: x1 = balance + 5 # x1 = 0 + 5 = 5<\/span>\n<span>t2: x2 = balance + 8 # x2 = 0 + 8 = 8<\/span>\n<span>t2: balance = x2 # balance = 8<\/span>\n<span>t1: balance = x1 # balance = 5<\/span>\n<span><\/span>\n<span>t1: x1 = balance - 5 # x1 = 5 - 5 = 0<\/span>\n<span>t1: balance = x1 # balance = 0<\/span>\n<span>t2: x2 = balance - 8 # x2 = 0 - 8 = -8<\/span>\n<span>t2: balance = x2 # balance = -8<\/span>\n<span><\/span>\n<span>\u7ed3\u679c balance = -8 <\/span>\n<\/pre>\n\n\n\n<p>\u7a76\u5176\u539f\u56e0\uff0c\u662f\u56e0\u4e3a\u4fee\u6539balance\u9700\u8981\u591a\u6761\u8bed\u53e5\uff0c\u800c\u6267\u884c\u8fd9\u51e0\u6761\u8bed\u53e5\u65f6\uff0c\u7ebf\u7a0b\u53ef\u80fd\u4e2d\u65ad\uff0c\u4ece\u800c\u5bfc\u81f4\u591a\u4e2a\u7ebf\u7a0b\u628a\u540c\u4e00\u4e2a\u5bf9\u8c61\u7684\u5185\u5bb9\u6539\u4e71\u4e86\u3002<\/p>\n<p>\u4e24\u4e2a\u7ebf\u7a0b\u540c\u65f6\u4e00\u5b58\u4e00\u53d6\uff0c\u5c31\u53ef\u80fd\u5bfc\u81f4\u4f59\u989d\u4e0d\u5bf9\uff0c\u4f60\u80af\u5b9a\u4e0d\u5e0c\u671b\u4f60\u7684\u94f6\u884c\u5b58\u6b3e\u83ab\u540d\u5176\u5999\u5730\u53d8\u6210\u4e86\u8d1f\u6570\uff0c\u6240\u4ee5\uff0c\u6211\u4eec\u5fc5\u987b\u786e\u4fdd\u4e00\u4e2a\u7ebf\u7a0b\u5728\u4fee\u6539balance\u7684\u65f6\u5019\uff0c\u522b\u7684\u7ebf\u7a0b\u4e00\u5b9a\u4e0d\u80fd\u6539\u3002<\/p>\n<p>\u5982\u679c\u6211\u4eec\u8981\u786e\u4fddbalance\u8ba1\u7b97\u6b63\u786e\uff0c\u5c31\u8981\u7ed9change_it()\u4e0a\u4e00\u628a\u9501\uff0c\u5f53\u67d0\u4e2a\u7ebf\u7a0b\u5f00\u59cb\u6267\u884cchange_it()\u65f6\uff0c\u6211\u4eec\u8bf4\uff0c\u8be5\u7ebf\u7a0b\u56e0\u4e3a\u83b7\u5f97\u4e86\u9501\uff0c\u56e0\u6b64\u5176\u4ed6\u7ebf\u7a0b\u4e0d\u80fd\u540c\u65f6\u6267\u884cchange_it()\uff0c\u53ea\u80fd\u7b49\u5f85\uff0c\u76f4\u5230\u9501\u88ab\u91ca\u653e\u540e\uff0c\u83b7\u5f97\u8be5\u9501\u4ee5\u540e\u624d\u80fd\u6539\u3002<\/p>\n<p>\u7531\u4e8e\u9501\u53ea\u6709\u4e00\u4e2a\uff0c\u65e0\u8bba\u591a\u5c11\u7ebf\u7a0b\uff0c\u540c\u4e00\u65f6\u523b\u6700\u591a\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u6301\u6709\u8be5\u9501\uff0c\u6240\u4ee5\uff0c\u4e0d\u4f1a\u9020\u6210\u4fee\u6539\u7684\u51b2\u7a81\u3002\u521b\u5efa\u4e00\u4e2a\u9501\u5c31\u662f\u901a\u8fc7threading.Lock()\u6765\u5b9e\u73b0\uff1a<\/p>\n\n\n\n<pre>\n<span>balance = 0<\/span>\n<span>lock = threading.Lock()<\/span>\n<span><\/span>\n<span>def run_thread(n):<\/span>\n<span>    for i in range(100000):<\/span>\n<span>        # \u5148\u8981\u83b7\u53d6\u9501:<\/span>\n<span>        lock.acquire()<\/span>\n<span>        try:<\/span>\n<span>            # \u653e\u5fc3\u5730\u6539\u5427:<\/span>\n<span>            change_it(n)<\/span>\n<span>        finally:<\/span>\n<span>            # \u6539\u5b8c\u4e86\u4e00\u5b9a\u8981\u91ca\u653e\u9501:<\/span>\n<span>            lock.release() <\/span>\n<\/pre>\n\n\n\n<p>\u5f53\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u6267\u884clock.acquire()\u65f6\uff0c\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u80fd\u6210\u529f\u5730\u83b7\u53d6\u9501\uff0c\u7136\u540e\u7ee7\u7eed\u6267\u884c\u4ee3\u7801\uff0c\u5176\u4ed6\u7ebf\u7a0b\u5c31\u7ee7\u7eed\u7b49\u5f85\u76f4\u5230\u83b7\u5f97\u9501\u4e3a\u6b62\u3002<\/p>\n<p>\u83b7\u5f97\u9501\u7684\u7ebf\u7a0b\u7528\u5b8c\u540e\u4e00\u5b9a\u8981\u91ca\u653e\u9501\uff0c\u5426\u5219\u90a3\u4e9b\u82e6\u82e6\u7b49\u5f85\u9501\u7684\u7ebf\u7a0b\u5c06\u6c38\u8fdc\u7b49\u5f85\u4e0b\u53bb\uff0c\u6210\u4e3a\u6b7b\u7ebf\u7a0b\u3002\u6240\u4ee5\u6211\u4eec\u7528try&#8230;finally\u6765\u786e\u4fdd\u9501\u4e00\u5b9a\u4f1a\u88ab\u91ca\u653e\u3002<\/p>\n<p>\u9501\u7684\u597d\u5904\u5c31\u662f\u786e\u4fdd\u4e86\u67d0\u6bb5\u5173\u952e\u4ee3\u7801\u53ea\u80fd\u7531\u4e00\u4e2a\u7ebf\u7a0b\u4ece\u5934\u5230\u5c3e\u5b8c\u6574\u5730\u6267\u884c\uff0c\u574f\u5904\u5f53\u7136\u4e5f\u5f88\u591a\uff0c\u9996\u5148\u662f\u963b\u6b62\u4e86\u591a\u7ebf\u7a0b\u5e76\u53d1\u6267\u884c\uff0c\u5305\u542b\u9501\u7684\u67d0\u6bb5\u4ee3\u7801\u5b9e\u9645\u4e0a\u53ea\u80fd\u4ee5\u5355\u7ebf\u7a0b\u6a21\u5f0f\u6267\u884c\uff0c\u6548\u7387\u5c31\u5927\u5927\u5730\u4e0b\u964d\u4e86\u3002<\/p>\n<p>\u5176\u6b21\uff0c\u7531\u4e8e\u53ef\u4ee5\u5b58\u5728\u591a\u4e2a\u9501\uff0c\u4e0d\u540c\u7684\u7ebf\u7a0b\u6301\u6709\u4e0d\u540c\u7684\u9501\uff0c\u5e76\u8bd5\u56fe\u83b7\u53d6\u5bf9\u65b9\u6301\u6709\u7684\u9501\u65f6\uff0c\u53ef\u80fd\u4f1a\u9020\u6210\u6b7b\u9501\uff0c\u5bfc\u81f4\u591a\u4e2a\u7ebf\u7a0b\u5168\u90e8\u6302\u8d77\uff0c\u65e2\u4e0d\u80fd\u6267\u884c\uff0c\u4e5f\u65e0\u6cd5\u7ed3\u675f\uff0c\u53ea\u80fd\u9760\u64cd\u4f5c\u7cfb\u7edf\u5f3a\u5236\u7ec8\u6b62\u3002<\/p>\n<h3>\u00a0<\/h3>\n<h3>\u591a\u6838CPU<\/h3>\n<p>\u5982\u679c\u4f60\u4e0d\u5e78\u62e5\u6709\u4e00\u4e2a\u591a\u6838CPU\uff0c\u4f60\u80af\u5b9a\u5728\u60f3\uff0c\u591a\u6838\u5e94\u8be5\u53ef\u4ee5\u540c\u65f6\u6267\u884c\u591a\u4e2a\u7ebf\u7a0b\u3002<br data-rich-text-line-break=\"true\" \/>\u5982\u679c\u5199\u4e00\u4e2a\u6b7b\u5faa\u73af\u7684\u8bdd\uff0c\u4f1a\u51fa\u73b0\u4ec0\u4e48\u60c5\u51b5\u5462\uff1f<\/p>\n<p>\u6253\u5f00Mac OS X\u7684Activity Monitor\uff0c\u6216\u8005Windows\u7684Task Manager\uff0c\u90fd\u53ef\u4ee5\u76d1\u63a7\u67d0\u4e2a\u8fdb\u7a0b\u7684CPU\u4f7f\u7528\u7387\u3002<\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u76d1\u63a7\u5230\u4e00\u4e2a\u6b7b\u5faa\u73af\u7ebf\u7a0b\u4f1a100%\u5360\u7528\u4e00\u4e2aCPU\u3002<\/p>\n<p>\u5982\u679c\u6709\u4e24\u4e2a\u6b7b\u5faa\u73af\u7ebf\u7a0b\uff0c\u5728\u591a\u6838CPU\u4e2d\uff0c\u53ef\u4ee5\u76d1\u63a7\u5230\u4f1a\u5360\u7528200%\u7684CPU\uff0c\u4e5f\u5c31\u662f\u5360\u7528\u4e24\u4e2aCPU\u6838\u5fc3\u3002<\/p>\n<p>\u8981\u60f3\u628aN\u6838CPU\u7684\u6838\u5fc3\u5168\u90e8\u8dd1\u6ee1\uff0c\u5c31\u5fc5\u987b\u542f\u52a8N\u4e2a\u6b7b\u5faa\u73af\u7ebf\u7a0b\u3002<\/p>\n<p>\u8bd5\u8bd5\u7528Python\u5199\u4e2a\u6b7b\u5faa\u73af\uff1a<\/p>\n\n\n\n<pre>\n<span>import threading, multiprocessing<\/span>\n<span><\/span>\n<span>def loop():<\/span>\n<span>    x = 0<\/span>\n<span>    while True:<\/span>\n<span>        x = x ^ 1<\/span>\n<span><\/span>\n<span>for i in range(multiprocessing.cpu_count()):<\/span>\n<span>    t = threading.Thread(target=loop)<\/span>\n<span>    t.start() <\/span>\n<\/pre>\n\n\n\n<p>\u542f\u52a8\u4e0eCPU\u6838\u5fc3\u6570\u91cf\u76f8\u540c\u7684N\u4e2a\u7ebf\u7a0b\uff0c\u57284\u6838CPU\u4e0a\u53ef\u4ee5\u76d1\u63a7\u5230CPU\u5360\u7528\u7387\u4ec5\u6709102%\uff0c\u4e5f\u5c31\u662f\u4ec5\u4f7f\u7528\u4e86\u4e00\u6838\u3002<\/p>\n<p>\u4f46\u662f\u7528C\u3001C++\u6216Java\u6765\u6539\u5199\u76f8\u540c\u7684\u6b7b\u5faa\u73af\uff0c\u76f4\u63a5\u53ef\u4ee5\u628a\u5168\u90e8\u6838\u5fc3\u8dd1\u6ee1\uff0c4\u6838\u5c31\u8dd1\u5230400%\uff0c8\u6838\u5c31\u8dd1\u5230800%\uff0c\u4e3a\u4ec0\u4e48Python\u4e0d\u884c\u5462\uff1f<\/p>\n<p>\u56e0\u4e3aPython\u7684\u7ebf\u7a0b\u867d\u7136\u662f\u771f\u6b63\u7684\u7ebf\u7a0b\uff0c\u4f46\u89e3\u91ca\u5668\u6267\u884c\u4ee3\u7801\u65f6\uff0c\u6709\u4e00\u4e2aGIL\u9501\uff1a<strong>Global Interpreter Lock<\/strong>\uff0c\u4efb\u4f55Python\u7ebf\u7a0b\u6267\u884c\u524d\uff0c\u5fc5\u987b\u5148\u83b7\u5f97GIL\u9501\uff0c\u7136\u540e\uff0c\u6bcf\u6267\u884c100\u6761\u5b57\u8282\u7801\uff0c\u89e3\u91ca\u5668\u5c31\u81ea\u52a8\u91ca\u653eGIL\u9501\uff0c\u8ba9\u522b\u7684\u7ebf\u7a0b\u6709\u673a\u4f1a\u6267\u884c\u3002<\/p>\n<p>\u8fd9\u4e2aGIL\u5168\u5c40\u9501\u5b9e\u9645\u4e0a\u628a\u6240\u6709\u7ebf\u7a0b\u7684\u6267\u884c\u4ee3\u7801\u90fd\u7ed9\u4e0a\u4e86\u9501\uff0c\u6240\u4ee5\uff0c\u591a\u7ebf\u7a0b\u5728Python\u4e2d\u53ea\u80fd\u4ea4\u66ff\u6267\u884c\uff0c\u5373\u4f7f100\u4e2a\u7ebf\u7a0b\u8dd1\u5728100\u6838CPU\u4e0a\uff0c\u4e5f\u53ea\u80fd\u7528\u52301\u4e2a\u6838\u3002<\/p>\n<p>GIL\u662fPython\u89e3\u91ca\u5668\u8bbe\u8ba1\u7684\u5386\u53f2\u9057\u7559\u95ee\u9898\uff0c\u901a\u5e38\u6211\u4eec\u7528\u7684\u89e3\u91ca\u5668\u662f\u5b98\u65b9\u5b9e\u73b0\u7684CPython\uff0c\u8981\u771f\u6b63\u5229\u7528\u591a\u6838\uff0c\u9664\u975e\u91cd\u5199\u4e00\u4e2a\u4e0d\u5e26GIL\u7684\u89e3\u91ca\u5668\u3002<\/p>\n<p>\u6240\u4ee5\uff0c\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u7ebf\u7a0b\uff0c\u4f46\u4e0d\u8981\u6307\u671b\u80fd\u6709\u6548\u5229\u7528\u591a\u6838\u3002\u5982\u679c\u4e00\u5b9a\u8981\u901a\u8fc7\u591a\u7ebf\u7a0b\u5229\u7528\u591a\u6838\uff0c\u90a3\u53ea\u80fd\u901a\u8fc7C\u6269\u5c55\u6765\u5b9e\u73b0\uff0c\u4e0d\u8fc7\u8fd9\u6837\u5c31\u5931\u53bb\u4e86Python\u7b80\u5355\u6613\u7528\u7684\u7279\u70b9\u3002<\/p>\n<p>\u4e0d\u8fc7\uff0c\u4e5f\u4e0d\u7528\u8fc7\u4e8e\u62c5\u5fc3\uff0cPython\u867d\u7136\u4e0d\u80fd\u5229\u7528\u591a\u7ebf\u7a0b\u5b9e\u73b0\u591a\u6838\u4efb\u52a1\uff0c\u4f46\u53ef\u4ee5\u901a\u8fc7\u591a\u8fdb\u7a0b\u5b9e\u73b0\u591a\u6838\u4efb\u52a1\u3002\u591a\u4e2aPython\u8fdb\u7a0b\u6709\u5404\u81ea\u72ec\u7acb\u7684GIL\u9501\uff0c\u4e92\u4e0d\u5f71\u54cd\u3002<\/p>\n<p>\u591a\u7ebf\u7a0b\u7f16\u7a0b\uff0c\u6a21\u578b\u590d\u6742\uff0c\u5bb9\u6613\u53d1\u751f\u51b2\u7a81\uff0c\u5fc5\u987b\u7528\u9501\u52a0\u4ee5\u9694\u79bb\uff0c\u540c\u65f6\uff0c\u53c8\u8981\u5c0f\u5fc3\u6b7b\u9501\u7684\u53d1\u751f\u3002<\/p>\n<p>Python\u89e3\u91ca\u5668\u7531\u4e8e\u8bbe\u8ba1\u65f6\u6709GIL\u5168\u5c40\u9501\uff0c\u5bfc\u81f4\u4e86\u591a\u7ebf\u7a0b\u65e0\u6cd5\u5229\u7528\u591a\u6838\u3002\u591a\u7ebf\u7a0b\u7684\u5e76\u53d1\u5728Python\u4e2d\u5c31\u662f\u4e00\u4e2a\u7f8e\u4e3d\u7684\u68a6\u3002<\/p>\n<p>\u4ee5\u4e0a\u8f6c\u8f7d\u81ea<a href=\"https:\/\/www.liaoxuefeng.com\/wiki\/1016959663602400\/1017627212385376\">\u5ed6\u96ea\u5cf0<\/a>\u3002<\/p>\n<p>\u00a0<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u6ca1\u6709\u98ce\u66b4\uff0c<\/p>\n<p>\u8239\u5e06\u4e0d\u8fc7\u662f\u4e00\u5757\u7834\u5e03\u3002<\/p>\n<p style=\"text-align: right;\">\u2014\u2014\u7ef4\u514b\u591a\u00b7\u96e8\u679c<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4ecb\u7ecd \u73b0\u4ee3\u64cd\u4f5c\u7cfb\u7edf\uff0c\u65e0\u8bba\u662f\u7535\u8111\u64cd\u4f5c\u7cfb\u7edf\uff08Mac OS\u3001Linux\u3001Windows\uff09\u8fd8\u662f\u624b\u673a\u64cd\u4f5c\u7cfb\u7edf\uff08IOS\u3001A [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1324,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[12,4],"tags":[37,22,15],"_links":{"self":[{"href":"https:\/\/www.yumefx.com\/index.php?rest_route=\/wp\/v2\/posts\/1245"}],"collection":[{"href":"https:\/\/www.yumefx.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yumefx.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yumefx.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yumefx.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1245"}],"version-history":[{"count":0,"href":"https:\/\/www.yumefx.com\/index.php?rest_route=\/wp\/v2\/posts\/1245\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.yumefx.com\/index.php?rest_route=\/wp\/v2\/media\/1324"}],"wp:attachment":[{"href":"https:\/\/www.yumefx.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1245"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yumefx.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1245"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yumefx.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1245"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}