{"id":78,"date":"2022-11-30T16:29:05","date_gmt":"2022-11-30T08:29:05","guid":{"rendered":"https:\/\/mianka.xyz\/?p=76"},"modified":"2022-11-30T16:38:29","modified_gmt":"2022-11-30T08:38:29","slug":"python","status":"publish","type":"post","link":"https:\/\/www.mianka.xyz\/?p=78","title":{"rendered":"python \u591a\u7ebf\u7a0b\u4e0e\u591a\u8fdb\u7a0b\u7684\u4ee3\u7801\u5b9e\u4f8b"},"content":{"rendered":"<h2>\u4e00.\u4e24\u8005\u533a\u522b<\/h2>\n<p>\u591a\u8fdb\u7a0b\u548c\u591a\u7ebf\u7a0b\u7684\u4e3b\u8981\u533a\u522b\u662f\uff1a\u7ebf\u7a0b\u662f\u8fdb\u7a0b\u7684\u5b50\u96c6\uff08\u90e8\u5206\uff09\uff0c\u4e00\u4e2a\u8fdb\u7a0b\u53ef\u80fd\u7531\u591a\u4e2a\u7ebf\u7a0b\u7ec4\u6210\u3002\u591a\u8fdb\u7a0b\u7684\u6570\u636e\u662f\u5206\u5f00\u7684\u3001\u5171\u4eab\u590d\u6742\uff0c\u9700\u8981\u7528IPC\uff1b\u4f46\u540c\u6b65\u7b80\u5355\u3002\u591a\u7ebf\u7a0b\u5171\u4eab\u8fdb\u7a0b\u6570\u636e\uff0c\u5171\u4eab\u7b80\u5355\uff1b\u4f46\u540c\u6b65\u590d\u6742\u3002<\/p>\n<p><\/p>\n<h3>\uff081\uff09\u591a\u8fdb\u7a0b<\/h3>\n<p><\/p>\n<p>\u8fdb\u7a0b\u662f\u7a0b\u5e8f\u5728\u8ba1\u7b97\u673a\u4e0a\u7684\u4e00\u6b21\u6267\u884c\u6d3b\u52a8\uff0c\u5373\u6b63\u5728\u8fd0\u884c\u4e2d\u7684\u5e94\u7528\u7a0b\u5e8f\uff0c\u901a\u5e38\u79f0\u4e3a\u8fdb\u7a0b\u3002\u5f53\u4f60\u8fd0\u884c\u4e00\u4e2a\u7a0b\u5e8f\uff0c\u4f60\u5c31\u542f\u52a8\u4e86\u4e00\u4e2a\u8fdb\u7a0b\u3002\u6bcf\u4e2a\u8fdb\u7a0b\u90fd\u6709\u81ea\u5df1\u72ec\u7acb\u7684\u5730\u5740\u7a7a\u95f4(\u5185\u5b58\u7a7a\u95f4)\uff0c\u6bcf\u5f53\u7528\u6237\u542f\u52a8\u4e00\u4e2a\u8fdb\u7a0b\u65f6\uff0c\u64cd\u4f5c\u7cfb\u7edf\u5c31\u4f1a\u4e3a\u8be5\u8fdb\u7a0b\u5206\u914d\u4e00\u4e2a\u72ec\u7acb\u7684\u5185\u5b58\u7a7a\u95f4\uff0c\u8ba9\u5e94\u7528\u7a0b\u5e8f\u5728\u8fd9\u4e2a\u72ec\u7acb\u7684\u5185\u5b58\u7a7a\u95f4\u4e2d\u8fd0\u884c\u3002<\/p>\n<p><\/p>\n<p>\u5728\u540c\u4e00\u4e2a\u65f6\u95f4\u91cc\uff0c\u540c\u4e00\u4e2a\u8ba1\u7b97\u673a\u7cfb\u7edf\u4e2d\u5982\u679c\u5141\u8bb8\u4e24\u4e2a\u6216\u4e24\u4e2a\u4ee5\u4e0a\u7684\u8fdb\u7a0b\u5904\u4e8e\u8fd0\u884c\u72b6\u6001\uff0c\u8fd9\u4fbf\u662f\u591a\u8fdb\u7a0b\uff0c\u4e5f\u79f0\u591a\u4efb\u52a1\u3002\u73b0\u4ee3\u7684\u64cd\u4f5c\u7cfb\u7edf\u51e0\u4e4e\u90fd\u662f\u591a\u4efb\u52a1\u64cd\u4f5c\u7cfb\u7edf\uff0c\u80fd\u591f\u540c\u65f6\u7ba1\u7406\u591a\u4e2a\u8fdb\u7a0b\u7684\u8fd0\u884c\u3002<\/p>\n<p><\/p>\n<p>\u591a\u4efb\u52a1\u5e26\u6765\u7684\u597d\u5904\u662f\u660e\u663e\u7684\uff0c\u6bd4\u5982\u4f60\u53ef\u4ee5\u8fb9\u542c\u97f3\u4e50\u8fb9\u6253\u6e38\u620f\uff0c\u4e0e\u6b64\u540c\u65f6\u751a\u81f3\u53ef\u4ee5\u5c06\u4e0b\u8f7d\u7684\u6587\u6863\u6253\u5370\u51fa\u6765\uff0c\u800c\u8fd9\u4e9b\u4efb\u52a1\u4e4b\u95f4\u4e1d\u6beb\u4e0d\u4f1a\u76f8\u4e92\u5e72\u6270\u3002<\/p>\n<p><\/p>\n<p>1.\u6570\u636e\u662f\u5206\u5f00\u7684\uff0c\u5171\u4eab\u590d\u6742\uff0c\u540c\u6b65\u7b80\u5355<\/p>\n<p>2.\u5360\u7528\u5185\u5b58\u591a\uff0c\u5207\u6362\u590d\u6742\uff0cCPU\u5229\u7528\u7387\u4f4e<\/p>\n<p>3.\u521b\u5efa\u9500\u6bc1\u590d\u6742\uff0c\u5207\u6362\u590d\u6742\uff0c\u901f\u5ea6\u6162<\/p>\n<p>4.\u7f16\u7a0b\u7b80\u5355\uff0c\u8c03\u8bd5\u7b80\u5355<\/p>\n<p>5.\u8fdb\u7a0b\u95f4\u4e0d\u4f1a\u76f8\u4e92\u5f71\u54cd<\/p>\n<p>6.\u9002\u5e94\u4e8e\u591a\u6838\u3001\u591a\u673a\u5206\u5e03 \uff1b\u5982\u679c\u4e00\u53f0\u673a\u5668\u4e0d\u591f\uff0c\u6269\u5c55\u5230\u591a\u53f0\u673a\u5668\u6bd4\u8f83\u7b80\u5355<\/p>\n<p><\/p>\n<h3>\uff082\uff09\u591a\u7ebf\u7a0b<\/h3>\n<p><\/p>\n<p>\u7ebf\u7a0b\u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684\u5b50\u8fdb\u7a0b\uff0c\u662f\u6700\u5c0f\u7684\u5904\u7406\u5355\u5143\uff1b\u662f\u4e00\u4e2a\u5355\u72ec\u7684\u6267\u884c\u8def\u5f84\u3002\u53ef\u4ee5\u8bf4\uff1a\u7ebf\u7a0b\u662f\u8fdb\u7a0b\u7684\u5b50\u96c6\uff08\u90e8\u5206\uff09\uff0c\u4e00\u4e2a\u8fdb\u7a0b\u53ef\u80fd\u7531\u591a\u4e2a\u7ebf\u7a0b\u7ec4\u6210\u3002<\/p>\n<p><\/p>\n<p>\u7ebf\u7a0b\u662f\u72ec\u7acb\u7684\u3002\u5982\u679c\u5728\u4e00\u4e2a\u7ebf\u7a0b\u4e2d\u53d1\u751f\u5f02\u5e38\uff0c\u5219\u4e0d\u4f1a\u5f71\u54cd\u5176\u4ed6\u7ebf\u7a0b\u3002\u5b83\u4f7f\u7528\u5171\u4eab\u5185\u5b58\u533a\u57df\u3002<\/p>\n<p><\/p>\n<p>\u591a\u7ebf\u7a0b\u662f\u4e00\u79cd\u6267\u884c\u6a21\u578b\uff0c\u5b83\u5141\u8bb8\u591a\u4e2a\u7ebf\u7a0b\u5b58\u5728\u4e8e\u8fdb\u7a0b\u7684\u4e0a\u4e0b\u6587\u4e2d\uff0c\u4ee5\u4fbf\u5b83\u4eec\u72ec\u7acb\u6267\u884c\u4f46\u5171\u4eab\u5176\u8fdb\u7a0b\u8d44\u6e90\u3002<\/p>\n<p><\/p>\n<p>1.\u591a\u7ebf\u7a0b\u5171\u4eab\u8fdb\u7a0b\u6570\u636e\uff0c\u5171\u4eab\u7b80\u5355\uff0c\u540c\u6b65\u590d\u6742<\/p>\n<p>2.\u5360\u7528\u5185\u5b58\u5c11\uff0c\u5207\u6362\u7b80\u5355\uff0cCPU\u5229\u7528\u7387\u9ad8<\/p>\n<p>3.\u521b\u5efa\u9500\u6bc1\u7b80\u5355\uff0c\u5207\u6362\u7b80\u5355\uff0c\u901f\u5ea6\u5feb<\/p>\n<p>4.\u7f16\u7a0b\u590d\u6742\uff0c\u8c03\u8bd5\u590d\u6742<\/p>\n<p>5.\u4e00\u4e2a\u7ebf\u7a0b\u6302\u6389\u5c06\u5bfc\u81f4\u6574\u4e2a\u8fdb\u7a0b\u6302\u6389<\/p>\n<p>6.\u9002\u5e94\u4e8e\u591a\u6838\u5206\u5e03<\/p>\n<h2>python\u591a\u8fdb\u7a0b\u4ee3\u7801\u5b9e\u4f8b<\/h2>\n<pre class=\"prism-highlight prism-language-python\">from&nbsp;multiprocessing&nbsp;import&nbsp;Process&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;\u591a\u8fdb\u7a0b\u7684\u7c7b\nimport&nbsp;time\nimport&nbsp;random\n&nbsp;\n&nbsp;\ndef&nbsp;test_fun(name):\n&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;\u968f\u673a\u7b49\u5f851~5\u79d2\n&nbsp;&nbsp;&nbsp;&nbsp;time.sleep(random.randrange(1,&nbsp;5))\n&nbsp;&nbsp;&nbsp;&nbsp;print(f&quot;\u6211\u662f{name}\u5b50\u8fdb\u7a0b\uff01&quot;)\n&nbsp;\n&nbsp;\n#&nbsp;\u8fdb\u7a0b\u4e00\u5b9a\u8981\u5199\u5728\u201cif&nbsp;__name__&nbsp;==&nbsp;&#39;__main__&#39;:\u201d\u4e0b\u9762\nif&nbsp;__name__&nbsp;==&nbsp;&#39;__main__&#39;:\n&nbsp;&nbsp;&nbsp;&nbsp;process_list&nbsp;=&nbsp;[]&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;\u5b58\u653e\u5f00\u542f\u7684\u8fdb\u7a0b\n&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;i&nbsp;in&nbsp;range(3):\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;\u8fdb\u7a0b\u4e2d\u7684\u53c2\u6570args\u8868\u793a\u8c03\u7528\u5bf9\u8c61\u7684\u4f4d\u7f6e\u53c2\u6570\u5143\u7ec4.\u6ce8\u610f\uff1a\u5143\u7ec4\u4e2d\u53ea\u6709\u4e00\u4e2a\u5143\u7d20\u65f6\u7ed3\u5c3e\u8981\u52a0&quot;,&quot;\u9017\u53f7\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p&nbsp;=&nbsp;Process(target=test_fun,&nbsp;args=(f&quot;son{i+1}&quot;,))\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p.start()\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;process_list.append(p)\n&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;i&nbsp;in&nbsp;process_list:\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i.join()&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;\u963b\u585e\u6bcf\u4e2a\u5b50\u8fdb\u7a0b\uff0c\u4e3b\u8fdb\u7a0b\u4f1a\u7b49\u5f85\u6240\u6709\u5b50\u8fdb\u7a0b\u7ed3\u675f\u518d\u7ed3\u675f\u4e3b\u8fdb\u7a0b\n&nbsp;&nbsp;&nbsp;&nbsp;print(&quot;\u4e3b\u8fdb\u7a0b\u7ed3\u675f\uff01&quot;)<\/pre>\n<h2>python\u591a\u7ebf\u7a0b\u4ee3\u7801\u5b9e\u4f8b<br \/><\/h2>\n<pre class=\"prism-highlight prism-language-python\">from&nbsp;threading&nbsp;import&nbsp;Thread\nimport&nbsp;time\ndef&nbsp;fun1():\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(&#39;fun11&#39;)\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;time.sleep(2)\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(&quot;fun1&quot;)\ndef&nbsp;fun2():\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(&#39;fin22&#39;)\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;time.sleep(6)\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(&quot;fun2&quot;)\nthreads&nbsp;=&nbsp;[]\nthreads.append(Thread(target=fun1))\nthreads.append(Thread(target=fun2))\nprint(threads)\nif&nbsp;__name__&nbsp;==&nbsp;&#39;__main__&#39;:\n&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;t&nbsp;in&nbsp;threads:\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(t)\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.start()<\/pre>\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00.\u4e24\u8005\u533a\u522b\u591a\u8fdb\u7a0b\u548c\u591a\u7ebf\u7a0b\u7684\u4e3b\u8981\u533a\u522b\u662f\uff1a\u7ebf\u7a0b\u662f\u8fdb\u7a0b\u7684\u5b50\u96c6\uff08\u90e8\u5206\uff09\uff0c\u4e00\u4e2a\u8fdb\u7a0b\u53ef\u80fd\u7531\u591a\u4e2a\u7ebf\u7a0b\u7ec4&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-78","post","type-post","status-publish","format-standard","hentry","category-pythonbiji"],"_links":{"self":[{"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=\/wp\/v2\/posts\/78","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=78"}],"version-history":[{"count":0,"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=\/wp\/v2\/posts\/78\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=78"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=78"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=78"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}