{"id":204778,"date":"2025-05-29T10:00:53","date_gmt":"2025-05-29T02:00:53","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204778\/"},"modified":"2025-05-29T10:00:53","modified_gmt":"2025-05-29T02:00:53","slug":"python-%e5%a4%9a%e8%bf%9b%e7%a8%8b%e9%80%9a%e4%bf%a1%e4%b8%ad%e7%ae%a1%e9%81%93%e5%b7%b2%e5%85%b3%e9%97%ad%e9%94%99%e8%af%af%ef%bc%9a%e5%a6%82%e4%bd%95%e8%a7%a3%e5%86%b3%e7%88%b6","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204778\/","title":{"rendered":"Python \u591a\u8fdb\u7a0b\u901a\u4fe1\u4e2d\u201c\u7ba1\u9053\u5df2\u5173\u95ed\u201d\u9519\u8bef\uff1a\u5982\u4f55\u89e3\u51b3\u7236\u5b50\u8fdb\u7a0b\u901a\u4fe1\u95ee\u9898\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Python \u591a\u8fdb\u7a0b\u901a\u4fe1\u4e2d\u201c\u7ba1\u9053\u5df2\u5173\u95ed\u201d\u9519\u8bef\uff1a\u5982\u4f55\u89e3\u51b3\u7236\u5b50\u8fdb\u7a0b\u901a\u4fe1\u95ee\u9898\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 \u591a\u8fdb\u7a0b\u901a\u4fe1\u4e2d\u201c\u7ba1\u9053\u5df2\u5173\u95ed\u201d\u9519\u8bef\uff1a\u5982\u4f55\u89e3\u51b3\u7236\u5b50\u8fdb\u7a0b\u901a\u4fe1\u95ee\u9898\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\/20241116\/17317287486738156ca6a39.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u201c\u7ba1\u9053\u5df2\u5173\u95ed\u201d\u9519\u8bef\u7684\u6839\u6e90<\/strong><\/p>\n<p>\u5f53\u4f7f\u7528 python \u7684 multiprocessing.pipe \u5efa\u7acb\u7236\u5b50\u8fdb\u7a0b\u901a\u4fe1\u65f6\uff0c\u5982\u679c\u4e00\u5f00\u59cb\u5c31\u51fa\u73b0\u201c\u7ba1\u9053\u5df2\u5173\u95ed\u201d\u7684\u9519\u8bef\uff0c\u901a\u5e38\u4e0e\u7ba1\u9053\u901a\u4fe1\u548c\u8fdb\u7a0b join \u65b9\u6cd5\u7684\u8c03\u7528\u6709\u5173\u3002<\/p>\n<p><strong>\u9020\u6210\u9519\u8bef\u7684\u539f\u56e0<\/strong><\/p>\n<p>\u95ee\u9898\u8be6\u60c5\u4e2d\u7ed9\u51fa\u7684\u4ee3\u7801\u4e2d\uff0c\u5b50\u8fdb\u7a0b\u5728 signal = child_conn.recv() \u5904\u8fdb\u5165\u8fdb\u7a0b\u963b\u585e\u72b6\u6001\uff0c\u7b49\u5f85\u4e3b\u8fdb\u7a0b\u53d1\u9001\u4fe1\u606f\u3002\u4f46\u662f\uff0c\u76f4\u5230\u4e3b\u8fdb\u7a0b\u9000\u51fa\u81ea\u5df1\u7684\u8fdb\u7a0b\uff0c\u5bf9\u7aef\u5173\u95ed\u8fde\u63a5\uff0c\u5b50\u8fdb\u7a0b\u8fd8\u6ca1\u6709\u6536\u5230\u9000\u51fa\u901a\u77e5\uff0c\u5bfc\u81f4\u81ea\u52a8\u89e6\u53d1\u901a\u9053\u5185\u6570\u636e\u7684\u5168\u91cf\u8bfb\u53d6\uff0c\u800c\u6b64\u65f6\u901a\u9053\u5df2\u5173\u95ed\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848<\/strong><\/p>\n<p>\u53ef\u4ee5\u901a\u8fc7\u5728 signal = child_conn.recv() \u4e0a\u6dfb\u52a0\u9519\u8bef\u6355\u83b7\u6765\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u5ffd\u7565\u9519\u8bef\u3002\u8fd9\u662f\u56e0\u4e3a\u4e3b\u8fdb\u7a0b\u4e0d\u53d1\u9001\u9000\u51fa\u901a\u77e5\u5c31\u9000\u51fa\u81ea\u5df1\u7684\u8fdb\u7a0b\u4e5f\u662f\u5408\u7406\u7684\u60c5\u51b5\u3002<\/p>\n<p><strong>\u4fee\u6539\u540e\u7684\u4ee3\u7801<\/strong><\/p>\n<p>\u5728 service.py \u4e2d\uff0c\u6dfb\u52a0\u9519\u8bef\u6355\u83b7\uff1a<\/p>\n<pre># catch IOError when main process automatically exit\ntry:\n    signal = child_conn.recv()\n    if signal:\n        child_conn.close()\nexcept EOFError as err:\n    print(err)<\/pre>\n<p><strong>\u5176\u4ed6\u6ce8\u610f\u4e8b\u9879<\/strong><\/p>\n<p>\u9664\u4e86\u9519\u8bef\u6355\u83b7\u4e4b\u5916\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u907f\u514d\u201c\u7ba1\u9053\u5df2\u5173\u95ed\u201d\u9519\u8bef\uff1a<\/p>\n<ul>\n<li>\u4ec5\u5728\u9700\u8981\u65f6\u8c03\u7528 server.stop()\uff0c\u56e0\u4e3a\u5b83\u5c06\u5173\u95ed\u7ba1\u9053\u5e76\u52a0\u5165\u5b50\u8fdb\u7a0b\u3002<\/li>\n<li>\u786e\u4fdd\u4e3b\u8fdb\u7a0b\u5728\u9000\u51fa\u524d\u59cb\u7ec8\u53d1\u9001\u9000\u51fa\u901a\u77e5\u5230\u5b50\u8fdb\u7a0b\u3002<\/li>\n<li>\u8003\u8651\u4f7f\u7528\u66f4\u5065\u58ee\u7684\u901a\u4fe1\u65b9\u6cd5\uff0c\u4f8b\u5982\u6d88\u606f\u961f\u5217\u6216\u8fdc\u7a0b\u8fc7\u7a0b\u8c03\u7528 (rpc)\u3002<\/li>\n<\/ul>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300aPython \u591a\u8fdb\u7a0b\u901a\u4fe1\u4e2d\u201c\u7ba1\u9053\u5df2\u5173\u95ed\u201d\u9519\u8bef\uff1a\u5982\u4f55\u89e3\u51b3\u7236\u5b50\u8fdb\u7a0b\u901a\u4fe1\u95ee\u9898\uff1f\u300b\uff0c\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u7ed9\u5927\u5bb6\u5206\u4eab\u66f4\u591a\u6587\u7ae0\u77e5\u8bc6\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python \u591a\u8fdb\u7a0b\u901a\u4fe1\u4e2d\u201c\u7ba1\u9053&#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-204778","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204778","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=204778"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204778\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204778"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204778"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204778"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}