{"id":1612,"date":"2019-10-11T19:55:07","date_gmt":"2019-10-11T11:55:07","guid":{"rendered":"http:\/\/ykyi.net\/?p=1612"},"modified":"2019-10-11T19:55:07","modified_gmt":"2019-10-11T11:55:07","slug":"python%e7%94%a8multiprocessing%e8%b5%b7%e5%ad%90%e8%bf%9b%e7%a8%8b%ef%bc%8c%e7%88%b6%e8%bf%9b%e7%a8%8b%e8%a2%ab%e6%9d%80%e6%8e%89%e6%97%b6%ef%bc%8c%e5%a6%82%e4%bd%95%e8%ae%a9%e5%ad%90%e8%bf%9b","status":"publish","type":"post","link":"https:\/\/ykyi.net\/?p=1612","title":{"rendered":"python\u7528multiprocessing\u8d77\u5b50\u8fdb\u7a0b\uff0c\u7236\u8fdb\u7a0b\u88ab\u6740\u6389\u65f6\uff0c\u5982\u4f55\u8ba9\u5b50\u8fdb\u7a0b\u4e5f\u81ea\u52a8\u9000\u51fa"},"content":{"rendered":"<p>\u6700\u8fd1\u7528python3\u5199\u4e86\u4e00\u6bb5\u4ee3\u7801\uff0c\u4ee3\u7801\u4f1a\u8d77\u4e00\u4e2a\u5b50\u8fdb\u7a0b\u505a\u4e00\u4e9b\u8f85\u52a9\u7684\u4e8b\u60c5\u3002\u4f46\u53d1\u73b0\u7236\u8fdb\u7a0b\u88ab\u6740\u6b7b\u540e\uff0c\u8fd9\u4e2a\u5b50\u8fdb\u7a0b\u53d8\u6210\u4e86\u5b64\u513f\u8fdb\u7a0b\uff0c\u4ecd\u7136\u5728\u8fd0\u884c\u3002<br \/>\n\u8d77\u8fdb\u7a0b\u7684\u4ee3\u7801\u5927\u6982\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python \">from multiprocessing import Process, Queue\n__q = Queue()\n__p = Process(target=__run, args=(__q,))\n__p.start()\n<\/code><\/pre>\n<p>\u5f88\u4e0d\u65b9\u4fbf\uff0c\u90a3\u600e\u6837\u8ba9\u7236\u8fdb\u7a0b\u9000\u51fa\u540e\uff0c\u5b50\u8fdb\u7a0b\u4e5f\u8ddf\u7740\u9000\u51fa\u5462\u3002<br \/>\n\u4e00\u756agoogle\u4e4b\u540e\uff0c\u539f\u6765\u53ef\u4ee5\u7528\u4e00\u884c\u4ee3\u7801\u89e3\u51b3\uff1a<\/p>\n<pre><code class=\"language-python \">from multiprocessing import Process, Queue\n__q = Queue()\n__p = Process(target=__run, args=(__q,))\n__p.daemon = True    # \u52a0\u4e0a\u8fd9\u4e00\u884c\u4ee3\u7801\n__p.start()\n<\/code><\/pre>\n<p>\u8bbe\u7f6e\u5b50\u8fdb\u7a0b\u7684daemon\u5c5e\u6027\u4e3aTrue\u3002\u8fd9\u6837\uff0c\u5f53\u5728\u63a7\u5236\u53f0\u4e0a\u7528ctrl-c\uff0c\u9000\u53d1\u9001SIGTERM\u6740\u6389\u7236\u8fdb\u7a0b\u540e\uff0c\u5b50\u8fdb\u7a0b\u4e5f\u4f1a\u8ddf\u7740\u9000\u51fa\uff0c\u4e0d\u4f1a\u6210\u4e3a\u5b64\u513f\u8fdb\u7a0b\u3002<br \/>\n\u53c2\u8003python\u5b98\u65b9\u7684\u89e3\u91ca\uff1a<\/p>\n<blockquote><p>\n  daemon<br \/>\n  The process\u2019s daemon flag, a Boolean value. This must be set before start() is called.<br \/>\n  The initial value is inherited from the creating process.<br \/>\n  When a process exits, it attempts to terminate all of its daemonic child processes.\n<\/p><\/blockquote>\n<p>\u5b98\u65b9\u89e3\u91ca\u7684\u91cd\u70b9\u662fdaemon flag\u5fc5\u987b\u5728start()\u524d\u88ab\u8bbe\u7f6e\u3002<br \/>\n\u8fd8\u6709\u4e00\u4e2a\u5751\u4eba\u7684\u5730\u65b9\uff0c\u8c8c\u4f3c\u7236\u8fdb\u7a0b\u5fc5\u987b\u88abSIGTERM\u4fe1\u53f7\u6740\u6b7b\uff0c\u6216\u8005\u81ea\u884c\u81ea\u7136\u9000\u51fa\uff0c\u5b50\u8fdb\u7a0b\u624d\u4f1a\u9000\u51fa\uff0c\u5426\u5219\u8fd8\u662f\u53d8\u6210\u4e86\u5b64\u513f\u8fdb\u7a0b\u3002\u5728stackoverflow\u4e0a\u6709\u4eba\u63d0\u5230\u4e86\u8fd9\u70b9:<br \/>\nhttps:\/\/stackoverflow.com\/questions\/25542110\/kill-child-process-if-parent-is-killed-in-python<\/p>\n<blockquote><p>\n  I want to mention, that if parent process was killed using SIGKILL (kill -9) then daemon processes won&#8217;t stop.\n<\/p><\/blockquote>\n<p>\u6211\u60f3\uff0c\u8fd9\u6216\u8bb8\u5e94\u8be5\u662f\u7531\u4e8e-9\uff08SIGKILL\uff09\u4f1a\u7acb\u523b\u4e2d\u6b62\u7236\u8fdb\u7a0b\uff0c\u4e8e\u662f\u7236\u8fdb\u7a0b\u6ca1\u6709\u673a\u4f1a\u901a\u77e5\u5b50\u8fdb\u7a0b\uff0c\u6bd4\u5982\u7528atexit\u8bbe\u7f6e\u7684\u8fdb\u7a0b\u9000\u51fa\u65f6\u8c03\u7528\u7684hook\u51fd\u6570\u5c31\u6ca1\u673a\u4f1a\u6267\u884c\u4e86\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u7528python3\u5199\u4e86\u4e00\u6bb5\u4ee3\u7801\uff0c\u4ee3\u7801\u4f1a\u8d77\u4e00\u4e2a\u5b50\u8fdb\u7a0b\u505a\u4e00\u4e9b\u8f85\u52a9\u7684\u4e8b\u60c5\u3002\u4f46\u53d1\u73b0\u7236\u8fdb\u7a0b\u88ab\u6740\u6b7b\u540e\uff0c\u8fd9\u4e2a\u5b50\u8fdb\u7a0b\u53d8\u6210\u4e86\u5b64\u513f\u8fdb\u7a0b\uff0c\u4ecd\u7136\u5728\u8fd0\u884c\u3002 \u8d77\u8fdb\u7a0b\u7684\u4ee3\u7801\u5927\u6982\u5982\u4e0b\uff1a from multiprocessing import Process, Queue __q = Queue() __p = Process(target=__run, args=(__q,)) __p.start() \u5f88\u4e0d\u65b9\u4fbf\uff0c\u90a3\u600e\u6837\u8ba9\u7236\u8fdb\u7a0b\u9000\u51fa\u540e\uff0c\u5b50\u8fdb\u7a0b\u4e5f\u8ddf\u7740\u9000\u51fa\u5462\u3002 \u4e00\u756agoogle\u4e4b\u540e\uff0c\u539f\u6765\u53ef\u4ee5\u7528\u4e00\u884c\u4ee3\u7801\u89e3\u51b3\uff1a from multiprocessing import Process, Queue __q = Queue() __p = Process(target=__run, args=(__q,)) __p.daemon = True # \u52a0\u4e0a\u8fd9\u4e00\u884c\u4ee3\u7801 __p.start() \u8bbe\u7f6e\u5b50\u8fdb\u7a0b\u7684daemon\u5c5e\u6027\u4e3aTrue\u3002\u8fd9\u6837\uff0c\u5f53\u5728\u63a7\u5236\u53f0\u4e0a\u7528ctrl-c\uff0c\u9000\u53d1\u9001SIGTERM\u6740\u6389\u7236\u8fdb\u7a0b\u540e\uff0c\u5b50\u8fdb\u7a0b\u4e5f\u4f1a\u8ddf\u7740\u9000\u51fa\uff0c\u4e0d\u4f1a\u6210\u4e3a\u5b64\u513f\u8fdb\u7a0b\u3002 \u53c2\u8003python\u5b98\u65b9\u7684\u89e3\u91ca\uff1a daemon The process\u2019s daemon flag, a Boolean value. This must be set before start() is called. The initial &hellip; <a href=\"https:\/\/ykyi.net\/?p=1612\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;python\u7528multiprocessing\u8d77\u5b50\u8fdb\u7a0b\uff0c\u7236\u8fdb\u7a0b\u88ab\u6740\u6389\u65f6\uff0c\u5982\u4f55\u8ba9\u5b50\u8fdb\u7a0b\u4e5f\u81ea\u52a8\u9000\u51fa&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[67],"class_list":["post-1612","post","type-post","status-publish","format-standard","hentry","category-tech_articles","tag-python"],"_links":{"self":[{"href":"https:\/\/ykyi.net\/index.php?rest_route=\/wp\/v2\/posts\/1612","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ykyi.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ykyi.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ykyi.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ykyi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1612"}],"version-history":[{"count":0,"href":"https:\/\/ykyi.net\/index.php?rest_route=\/wp\/v2\/posts\/1612\/revisions"}],"wp:attachment":[{"href":"https:\/\/ykyi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1612"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ykyi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1612"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ykyi.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1612"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}