{"id":204575,"date":"2025-05-29T12:25:30","date_gmt":"2025-05-29T04:25:30","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204575\/"},"modified":"2025-05-29T12:25:30","modified_gmt":"2025-05-29T04:25:30","slug":"python%e8%bf%9b%e7%a8%8b%e6%b1%a0%e5%a6%82%e4%bd%95%e5%88%9b%e5%bb%ba%e5%ad%90%e8%bf%9b%e7%a8%8b%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204575\/","title":{"rendered":"Python\u8fdb\u7a0b\u6c60\u5982\u4f55\u521b\u5efa\u5b50\u8fdb\u7a0b\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Python\u8fdb\u7a0b\u6c60\u5982\u4f55\u521b\u5efa\u5b50\u8fdb\u7a0b\uff1f<\/h1>\n<p>\u6765\u5230\u7684\u5927\u5bb6\uff0c\u76f8\u4fe1\u90fd\u662f\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5e0c\u671b\u5728\u8fd9\u91cc\u5b66\u4e60\u6587\u7ae0\u76f8\u5173\u7f16\u7a0b\u77e5\u8bc6\u3002\u4e0b\u9762\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u5e26\u5927\u5bb6\u804a\u804a\u300aPython\u8fdb\u7a0b\u6c60\u5982\u4f55\u521b\u5efa\u5b50\u8fdb\u7a0b\uff1f\u300b\uff0c\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241111\/173128486567314f813e116.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>python\u8fdb\u7a0b\u6c60\u65e0\u6cd5\u521b\u5efa\u5b50\u8fdb\u7a0b\u7684\u89e3\u51b3\u4e4b\u9053<\/strong><\/p>\n<p>\u5728\u591a\u4efb\u52a1\u5904\u7406\u4e2d\uff0c\u4f7f\u7528\u8fdb\u7a0b\u6c60\u80fd\u6709\u6548\u907f\u514d\u7cfb\u7edf\u8fdb\u7a0b\u6570\u91cf\u9650\u5236\u3002\u7136\u800c\uff0c\u5f53\u7279\u5b9a\u4efb\u52a1\u9700\u8981\u5b50\u8fdb\u7a0b\u521b\u5efa\u5b50\u8fdb\u7a0b\u65f6\uff0c\u4f7f\u7528\u8fdb\u7a0b\u6c60\u53ef\u80fd\u4f1a\u53d7\u9650\u3002\u672c\u6587\u5c06\u63a2\u8ba8\u5982\u4f55\u5728\u8fdb\u7a0b\u6c60\u4e2d\u5b9e\u73b0\u5b50\u8fdb\u7a0b\u521b\u5efa\u5b50\u8fdb\u7a0b\u3002<\/p>\n<p>\u5728\u7ed9\u5b9a\u7684python\u4ee3\u7801\u4e2d\uff0c<strong>print_log()<\/strong>\u51fd\u6570\u8bd5\u56fe\u5728\u8fdb\u7a0b\u6c60\u4e2d\u521b\u5efa\u5b50\u8fdb\u7a0b\uff0c\u4f46\u7531\u4e8e\u4ee5\u4e0b\u539f\u56e0\u5931\u8d25\uff1a<\/p>\n<ul>\n<li>\u8fdb\u7a0b\u6c60\u53ea\u5141\u8bb8\u521b\u5efa\u8fdb\u7a0b\uff0c\u800c\u4e0d\u662f\u5b50\u8fdb\u7a0b\u3002<\/li>\n<\/ul>\n<p>\u8981\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u6709\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a<\/p>\n<p><strong>1. \u4f7f\u7528multiprocessing.manager()<\/strong><\/p>\n<ul>\n<li>multiprocessing.manager()\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5171\u4eab\u5185\u5b58\u7ba1\u7406\u5668\uff0c\u5141\u8bb8\u5728\u8fdb\u7a0b\u4e4b\u95f4\u5171\u4eab\u6570\u636e\u3002<\/li>\n<li>\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u5b50\u8fdb\u7a0b\u5e76\u5728\u5171\u4eab\u5185\u5b58\u4e2d\u5b58\u50a8\u5176pid\u3002<\/li>\n<li>\u4e3b\u8fdb\u7a0b\u53ef\u4ee5\u68c0\u7d22pid\u5e76\u4f7f\u7528os.fork()\u521b\u5efa\u5b59\u5b50\u8fdb\u7a0b\u3002<\/li>\n<\/ul>\n<p><strong>2. \u4f7f\u7528concurrent.futures.threadpoolexecutor()<\/strong><\/p>\n<ul>\n<li>threadpoolexecutor\u4f7f\u7528\u7ebf\u7a0b\u800c\u4e0d\u662f\u8fdb\u7a0b\u6765\u6267\u884c\u4efb\u52a1\u3002<\/li>\n<li>\u7531\u4e8e\u7ebf\u7a0b\u5171\u4eab\u76f8\u540c\u7684\u5185\u5b58\u7a7a\u95f4\uff0c\u56e0\u6b64\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u521b\u5efa\u5b59\u5b50\u8fdb\u7a0b\uff1a<\/li>\n<\/ul>\n<pre>import concurrent.futures\n\ndef print_run(msg):\n    print('msg.%s' % msg)\n    time.sleep(2)\n\ndef print_log(msg):\n    with concurrent.futures.threadpoolexecutor() as executor:\n        future = executor.submit(print_run, msg)\n        future.result()\n        print('msg is : %s' % msg)\n        time.sleep(1)\n\nif __name__ == '__main__':\n    for i in range(20):\n        print_log(str(i))<\/pre>\n<p><strong>3. \u4f7f\u7528subprocess.popen()<\/strong><\/p>\n<ul>\n<li>subprocess.popen()\u53ef\u4ee5\u7528\u4e8e\u521b\u5efa\u5b50\u8fdb\u7a0b\uff0c\u5305\u62ec\u4ece\u5b50\u8fdb\u7a0b\u4e2d\u521b\u5efa\u5b59\u5b50\u8fdb\u7a0b\u3002<\/li>\n<\/ul>\n<pre>import subprocess\n\ndef print_run(msg):\n    print('msg.%s' % msg)\n    time.sleep(2)\n\ndef print_log(msg):\n    p = subprocess.Popen(['python', '-c', 'import time; time.sleep(2); print(\"msg.%s\")' % msg])\n    p.wait()\n    print('msg is : %s' % msg)\n    time.sleep(1)\n\nif __name__ == '__main__':\n    for i in range(20):\n        print_log(str(i))<\/pre>\n<p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728\u8fdb\u7a0b\u6c60\u4e2d\u5b9e\u73b0\u5b50\u8fdb\u7a0b\u521b\u5efa\u5b50\u8fdb\u7a0b\uff0c\u4ece\u800c\u6ee1\u8db3\u7279\u5b9a\u7684\u4efb\u52a1\u9700\u6c42\u3002<\/p>\n<p>\u672c\u7bc7\u5173\u4e8e\u300aPython\u8fdb\u7a0b\u6c60\u5982\u4f55\u521b\u5efa\u5b50\u8fdb\u7a0b\uff1f\u300b\u7684\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\u5566\uff0c\u4f46\u662f\u5b66\u65e0\u6b62\u5883\uff0c\u60f3\u8981\u4e86\u89e3\u5b66\u4e60\u66f4\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python\u8fdb\u7a0b\u6c60\u5982\u4f55\u521b\u5efa\u5b50\u8fdb\u7a0b&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-204575","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204575","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=204575"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204575\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204575"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204575"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204575"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}