{"id":1573,"date":"2024-01-25T21:53:20","date_gmt":"2024-01-25T12:53:20","guid":{"rendered":"https:\/\/ryo-fujinone.net\/blog\/?p=1573"},"modified":"2024-01-26T21:36:42","modified_gmt":"2024-01-26T12:36:42","slug":"post-1573","status":"publish","type":"post","link":"https:\/\/ryo-fujinone.net\/blog\/archives\/1573","title":{"rendered":"Python\u306esubprocess\u3067\u6a19\u6e96\u51fa\u529b\u3092\u30ea\u30a2\u30eb\u30bf\u30a4\u30e0\u306b\u53d6\u5f97\u3057\u3064\u3064threading.Timer\u3068psutil\u3067\u30bf\u30a4\u30e0\u30a2\u30a6\u30c8\u3092\u5b9f\u88c5\u3057\u3066\u307f\u308b"},"content":{"rendered":"\n<p>Python\u306e<a rel=\"noreferrer noopener\" href=\"https:\/\/docs.python.org\/ja\/3\/library\/subprocess.html\" data-type=\"link\" data-id=\"https:\/\/docs.python.org\/ja\/3\/library\/subprocess.html\" target=\"_blank\">subprocess<\/a>\u3067\u4f55\u304b\u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3059\u308b\u969b\u306b\u30bf\u30a4\u30e0\u30a2\u30a6\u30c8\u51e6\u7406\u3092\u5b9f\u88c5\u3057\u305f\u3044\u3053\u3068\u304c\u3042\u308b\u3068\u601d\u3044\u307e\u3059\u3002subprocess\u306frun()\u3001Popen.communicate()\u3001call()\u3001check_call()\u3001check_output()\u3067\u30bf\u30a4\u30e0\u30a2\u30a6\u30c8\u306e\u79d2\u6570\u3092\u8a2d\u5b9a\u3067\u304d\u307e\u3059\u304c\u3001\u3069\u308c\u3082\u5b50\u30d7\u30ed\u30bb\u30b9\u306e\u6a19\u6e96\u51fa\u529b\u3092\u30ea\u30a2\u30eb\u30bf\u30a4\u30e0\u3067\u53d6\u5f97\u3059\u308b\u3053\u3068\u306f\u3067\u304d\u307e\u305b\u3093\u3002<\/p>\n\n\n\n<p>run()\u3067\u306etimeout\u5024\u306e\u6307\u5b9a\u306f\u5185\u90e8\u7684\u306b\u306fcommunicate()\u306e\u4f7f\u7528\u3068\u540c\u3058\u3067\u3001check_output()\u306frun()\u3067check=True\u306b\u3059\u308c\u3070\u540c\u3058\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u305d\u308c\u3089\u306f\u30b3\u30de\u30f3\u30c9\u304c\u7d42\u4e86\u3059\u308b\u307e\u3067\u6a19\u6e96\u51fa\u529b\u306e\u7d50\u679c\u3092\u53d7\u3051\u53d6\u308c\u307e\u305b\u3093\u3002\u305d\u3057\u3066call()\u3001check_call()\u306f\u6a19\u6e96\u51fa\u529b\u3092\u53d6\u5f97\u3059\u308b\u76ee\u7684\u306e\u95a2\u6570\u3067\u306f\u306a\u304f\u3001\u666e\u901a\u306b\u4f7f\u3046\u3068\u30b3\u30de\u30f3\u30c9\u306e\u5b9f\u884c\u7d50\u679c\u304c\u30b3\u30de\u30f3\u30c9\u306e\u7d42\u4e86\u3092\u5f85\u305f\u305a\u30ea\u30a2\u30eb\u30bf\u30a4\u30e0\u306b\u30b7\u30a7\u30eb\u306b\u51fa\u529b\u3055\u308c\u307e\u3059\u304c\u3001\u305d\u308c\u306f\u5185\u90e8\u7684\u306b\u4f7f\u7528\u3055\u308c\u3066\u3044\u308bPopen\u3067stdout\/stderr\u306b\u4f55\u3082\u6307\u5b9a\u3055\u308c\u3066\u3044\u306a\u3044\u305f\u3081\u3067\u3059\u3002\u305d\u3093\u306a\u611f\u3058\u3067\u30bf\u30a4\u30e0\u30a2\u30a6\u30c8\u3092\u30b5\u30dd\u30fc\u30c8\u3057\u305fsubprocess\u306e\u95a2\u6570\u306f\u6a19\u6e96\u51fa\u529b\u306e\u30ea\u30a2\u30eb\u30bf\u30a4\u30e0\u53d6\u5f97\u306b\u306f\u6d3b\u304b\u305b\u305d\u3046\u306b\u3042\u308a\u307e\u305b\u3093\u3002\u306a\u304a\u5f8c\u8ff0\u3057\u307e\u3059\u304cPopen.wait()\u306e\u30bf\u30a4\u30e0\u30a2\u30a6\u30c8\u306b\u3064\u3044\u3066\u306f\u6a19\u6e96\u51fa\u529b\u306e\u30ea\u30a2\u30eb\u30bf\u30a4\u30e0\u53d6\u5f97\u3068\u306f\u76f8\u6027\u304c\u826f\u304f\u306a\u3044\u3067\u3059\u3002<\/p>\n\n\n\n<p>\u3053\u306e\u8a18\u4e8b\u3067\u306fsubprocess.Popen\u3092\u4f7f\u7528\u3057\u3066\u6a19\u6e96\u51fa\u529b\u3092\u30ea\u30a2\u30eb\u30bf\u30a4\u30e0\u306b\u53d6\u5f97\u3059\u308b\u3053\u3068\u3092\u524d\u63d0\u3068\u3057\u305f\u4e0a\u3067\u3001\u30bf\u30a4\u30e0\u30a2\u30a6\u30c8\u306b\u3088\u308b\u5b50\u30d7\u30ed\u30bb\u30b9\u306ekill\u3092\u5b9f\u88c5\u3057\u3066\u307f\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u30bf\u30a4\u30e0\u30a2\u30a6\u30c8\u306e\u30bf\u30a4\u30de\u30fc\u306f<a rel=\"noreferrer noopener\" href=\"https:\/\/docs.python.org\/ja\/3\/library\/threading.html\" data-type=\"link\" data-id=\"https:\/\/docs.python.org\/ja\/3\/library\/threading.html\" target=\"_blank\">threading<\/a>\u30e2\u30b8\u30e5\u30fc\u30eb\u306eTimer\u3092\u7528\u3044\u3066\u5b9f\u88c5\u3057\u307e\u3059\u3002\u30c7\u30b3\u30ec\u30fc\u30bf\u3067\u30bf\u30a4\u30e0\u30a2\u30a6\u30c8\u304c\u5b9f\u88c5\u3067\u304d\u308b<a href=\"https:\/\/pypi.org\/project\/wrapt-timeout-decorator\/\" data-type=\"link\" data-id=\"https:\/\/pypi.org\/project\/wrapt-timeout-decorator\/\" target=\"_blank\" rel=\"noreferrer noopener\">wrapt-timeout-decorator<\/a>\u3082\u4f7f\u3048\u308b\u3053\u3068\u306f\u78ba\u8a8d\u3057\u307e\u3057\u305f\u304c\u3001\u3069\u3046\u3082Windows\u74b0\u5883\u3067\u306f\u66f8\u304d\u65b9\u304c\u5236\u9650\u3055\u308c\u308b\u3063\u307d\u3044\u306e\u3068\u3001Timer\u306e\u66f8\u304d\u65b9\u306e\u65b9\u304c\u500b\u4eba\u7684\u306b\u597d\u307f\u306a\u306e\u3067Timer\u3092\u4f7f\u3046\u3053\u3068\u306b\u3057\u307e\u3057\u305f\u3002<\/p>\n\n\n\n<p>\u30d7\u30ed\u30bb\u30b9\u306ekill\u306b\u3064\u3044\u3066\u306f<a rel=\"noreferrer noopener\" href=\"https:\/\/pypi.org\/project\/psutil\/\" data-type=\"link\" data-id=\"https:\/\/psutil.readthedocs.io\/en\/latest\/index.html\" target=\"_blank\">psutil<\/a>\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002\u3053\u308c\u306fkill\u306e\u66f8\u304d\u65b9\u3084\u5b50\u5b6b\u30d7\u30ed\u30bb\u30b9\u306ekill\u307e\u3067\u3092\u8003\u3048\u305f\u6642\u306bOS(UNIX\u3001Windows)\u306e\u9055\u3044\u3084exec\u30b3\u30de\u30f3\u30c9\u306e\u6709\u7121\u7b49\u3067\u60a9\u307e\u306a\u304f\u3066\u826f\u3044\u3088\u3046\u306b\u3059\u308b\u305f\u3081\u3067\u3059\u3002<br>subprocess.Popen.kill()\u3067\u306f\u5b50\u5b6b\u30d7\u30ed\u30bb\u30b9\u307e\u3067kill\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u306a\u3044\u305f\u3081\u3001\u30bf\u30a4\u30e0\u30a2\u30a6\u30c8\u3092\u5b9f\u88c5\u3059\u308b\u306a\u3089psutil\u3092\u4f7f\u3046\u306e\u304c\u697d\u3067\u3059\u3002\u3053\u306e\u8a18\u4e8b\u3067\u306fpsutil\u306echildren()\u3067\u5b50\u5b6b\u30d7\u30ed\u30bb\u30b9\u306e\u53d6\u5f97\u3092\u4f53\u9a13\u3059\u308b\u305f\u3081\u306bsubprocess\u306bshell=True\u3092\u8a2d\u5b9a\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u74b0\u5883\u306f Windows 10\u3001Python 3.12.1<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">sub.py (\u51fa\u529b\u30c6\u30b9\u30c8\u7528)<\/h2>\n\n\n\n<p>\u307e\u305a\u30c6\u30b9\u30c8\u7528\u306b\u4ee5\u4e0b\u306e\u3088\u3046\u306asub.py\u3092\u7528\u610f\u3057\u307e\u3057\u305f\u3002print\u3067\u51fa\u529b\u3057\u305f\u30891\u79d2\u5f85\u3063\u3066\u7e70\u308a\u8fd4\u3059\u3060\u3051\u3067\u3059\u3002subprocess\u3092\u4f7f\u7528\u3057\u3066\u30ea\u30a2\u30eb\u30bf\u30a4\u30e0\u306b\u6a19\u6e96\u51fa\u529b\u3092\u53d7\u3051\u53d6\u308c\u308b\u3088\u3046\u306bprint\u3067\u306fflush=True\u3092\u8a2d\u5b9a\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>import time\n\nfor i in range(0, 10):\n    print(i, flush=True)\n    time.sleep(1)<\/code><\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">main.py (\u6a19\u6e96\u51fa\u529b\u306e\u30ea\u30a2\u30eb\u30bf\u30a4\u30e0\u53d6\u5f97)<\/h2>\n\n\n\n<p>\u6a19\u6e96\u51fa\u529b\u306e\u30ea\u30a2\u30eb\u30bf\u30a4\u30e0\u53d6\u5f97\u306e\u78ba\u8a8d\u306e\u305f\u3081\u306b\u4ee5\u4e0b\u306e\u3088\u3046\u306amain.py\u3092\u7528\u610f\u3057\u307e\u3057\u305f\u3002\u3053\u3053\u3067\u306f\u6a19\u6e96\u30a8\u30e9\u30fc\u51fa\u529b\u3092\u6a19\u6e96\u51fa\u529b\u3068\u3057\u3066\u53d7\u3051\u53d6\u308c\u308b\u3088\u3046\u306bstderr=subprocess.STDOUT\u306b\u3057\u3066\u3044\u307e\u3059\u304c\u3001stderr\u304c\u4e0d\u8981\u306a\u5834\u5408\u3084stderr\u3092\u5206\u3051\u3066\u53d6\u5f97\u3059\u308b\u5834\u5408\u7b49\u3001\u30b1\u30fc\u30b9\u30d0\u30a4\u30b1\u30fc\u30b9\u3067\u3059\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>import subprocess\n\ndef main(cmd):\n    proc = subprocess.Popen(\n        cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT\n    )\n\n    for line in iter(proc.stdout.readline, b&quot;&quot;):\n        line = line.rstrip().decode()\n        print(line)\n\n    proc.wait()\n\nif __name__ == &quot;__main__&quot;:\n    cmd = &quot;python sub.py&quot;\n    main(cmd)<\/code><\/pre><\/div>\n\n\n\n<p>\u3053\u3053\u3067for\u306e\u4e2d\u3067\u30bf\u30a4\u30e0\u30a2\u30a6\u30c8\u306bPopen.wait()\u304c\u4f7f\u3048\u306a\u304f\u306f\u306a\u3044\u3067\u3059\u3002\u305f\u3060\u4f7f\u3063\u305f\u6642\u70b9\u3067\u30ea\u30a2\u30eb\u30bf\u30a4\u30e0\u53d6\u5f97\u304c\u3067\u304d\u306a\u304f\u306a\u308b\u306e\u3067\u3001\u3084\u308b\u306a\u3089\u4f55\u304b\u3057\u3089\u306e\u6761\u4ef6\u3092\u8a2d\u3051\u305f\u4e0a\u3067wait()\u3092\u4f7f\u3044\u3001\u305d\u308c\u4ee5\u964d\u306e\u30ea\u30a2\u30eb\u30bf\u30a4\u30e0\u53d6\u5f97\u306f\u8ae6\u3081\u3066\u6b63\u5e38\u306b\u7d42\u4e86\u3059\u308b\u306e\u3092\u5f85\u3064\u304bTimeoutExpired\u3092\u5f85\u3064\u304b\u3068\u3044\u3046\u611f\u3058\u306b\u306a\u308b\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<div class=\"wp-block-luxe-blocks-border-block\" style=\"margin-top:10px;margin-bottom:30px\"><div class=\"wp-block-luxe-blocks-border-block-content\" style=\"border:1px solid #006edc;border-radius:0px;padding:0px 15px 0px 15px\">\n<p><span style=\"text-decoration: underline;\">\u88dc\u8db3<\/span><br>\u6a19\u6e96\u51fa\u529b\u3068\u6a19\u6e96\u30a8\u30e9\u30fc\u51fa\u529b\u3092\u5b8c\u5168\u306b\u5206\u3051\u3066\u53d6\u5f97\u3057\u305f\u3044\u5834\u5408\uff08stdout=PIPE, stderr=PIPE\uff09\u3001\u72b6\u6cc1\u6b21\u7b2c\u3067\u306f\u30d1\u30a4\u30d7\u30d0\u30c3\u30d5\u30a1\u304c\u8a70\u307e\u308a\u30c7\u30c3\u30c9\u30ed\u30c3\u30af\u304c\u767a\u751f\u3057\u307e\u3059\u3002\u5bfe\u51e6\u6cd5\u7b49\u306f\u4ee5\u4e0b\u304c\u53c2\u8003\u306b\u306a\u308b\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002<br><a href=\"https:\/\/stackoverflow.com\/questions\/18421757\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/stackoverflow.com\/questions\/18421757<\/a><br><a href=\"https:\/\/akirayou.net\/wp\/2021\/python-%E3%81%AEsubprocess\/\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/akirayou.net\/wp\/2021\/python-%E3%81%AEsubprocess\/<\/a><br>\u30c7\u30c3\u30c9\u30ed\u30c3\u30af\u3092\u56de\u907f\u3057\u3064\u3064\u6a19\u6e96\u51fa\u529b\u3068\u6a19\u6e96\u30a8\u30e9\u30fc\u51fa\u529b\u306e\u4e21\u65b9\u3067\u30ea\u30a2\u30eb\u30bf\u30a4\u30e0\u6027\u306b\u62d8\u308b\u306e\u3067\u3042\u308c\u3070\u3001\u500b\u4eba\u7684\u306b\u306f<a href=\"https:\/\/stackoverflow.com\/a\/57084403\" target=\"_blank\" rel=\"noreferrer noopener\">Stack Overflow\u306e\u30a2\u30f3\u30b5\u30fc\u306e1\u3064<\/a>\u306b\u3082\u3042\u308b\u3088\u3046\u306bThreadPoolExecutor\u3092\u4f7f\u3046\u306e\u304c\u697d\u3060\u3068\u601d\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">threading.Timer\u3067\u30bf\u30a4\u30de\u30fc\u306e\u5b9f\u88c5<\/h2>\n\n\n\n<p>\u4e0a\u306emain.py\u306bthreading.Timer\u3067\u30bf\u30a4\u30de\u30fc\u3092\u8ffd\u52a0\u3059\u308b\u3068\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u5f62\u306b\u306a\u308a\u307e\u3059\u3002Timer\u306b\u8a2d\u5b9a\u3057\u305fkill_process()\u304c\u6307\u5b9a\u306e\u79d2\u5f8c\u306b\u6a5f\u80fd\u3059\u308b\u3088\u3046\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002\u3053\u3053\u304b\u3089\u306f\u597d\u304d\u306bkill\u3059\u308c\u3070\u826f\u3044\u306e\u3067\u3059\u304c\u3001\u3053\u306e\u8a18\u4e8b\u3067\u306fpsutil\u3092\u4f7f\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>from threading import Timer\n\ndef main(cmd, timeout=3):\n    proc = subprocess.Popen(\n        cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT\n    )\n\n    def kill_process(pid):\n        print(pid)\n        # \u3053\u3053\u306bkill\u306e\u51e6\u7406\u3092\u66f8\u304f\n\n    t = Timer(timeout, kill_process, [proc.pid])\n    t.start()\n\n    for line in iter(proc.stdout.readline, b&quot;&quot;):\n        line = line.rstrip().decode()\n        print(line)\n\n    t.cancel()\n    proc.stdout.close()\n    proc.wait()<\/code><\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">\u30d7\u30ed\u30bb\u30b9\u306ekill<\/h2>\n\n\n\n<p>kill_process()\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u30b3\u30fc\u30c9\u306b\u3057\u3066\u307f\u307e\u3057\u305f\u3002<br>\u203bkill_process()\u3068killed()\u306f\u4e0a\u306e\u30b3\u30fc\u30c9\u306emain()\u306e\u4e2d\u306b\u66f8\u3044\u3066\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>import psutil\n\n\ndef killed(proc):\n    print(f&quot;process {proc} killed with exit code {proc.returncode}&quot;)\n\ndef kill_process(pid):\n    t.cancel()\n    try:\n        parent = psutil.Process(pid)\n    except psutil.NoSuchProcess as e:\n        print(e)\n        return\n\n    procs = parent.children(recursive=True)\n    procs.append(parent)\n    for p in procs:\n        try:\n            p.kill()\n        except Exception as e:\n            print(e)\n    gone, alive = psutil.wait_procs(procs, callback=killed)<\/code><\/pre><\/div>\n\n\n\n<p>\u307e\u305a\u30bf\u30a4\u30de\u30fc\u3092cancel()\u3067\u7d42\u4e86\u3057\u3066\u304a\u304d\u307e\u3059\u3002main()\u306e\u6700\u5f8c\u306e\u65b9\u3067\u3082\u66f8\u3044\u3066\u3044\u308b\u306e\u3067\u3053\u3053\u3067\u306f\u66f8\u304b\u306a\u304f\u3066\u3082\u554f\u984c\u306f\u306a\u3044\u306e\u3067\u3059\u304c\u3001\u30bf\u30a4\u30e0\u30a2\u30a6\u30c8\u306e\u6642\u70b9\u3067\u5373\u30bf\u30a4\u30de\u30fc\u3092\u7d42\u4e86\u3055\u305b\u3066\u304a\u304d\u305f\u3044\u3068\u3044\u3046\u5834\u5408\u306f\u3053\u3046\u3044\u3046\u66f8\u304d\u65b9\u306b\u306a\u308b\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<p>subprocess\u306e\u30d7\u30ed\u30bb\u30b9ID\u3092 psutil.Process() \u306b\u6e21\u3057\u3066\u305d\u306e\u30d7\u30ed\u30bb\u30b9\u3092\u53d6\u5f97\u3057\u307e\u3059\u3002\u30d7\u30ed\u30bb\u30b9\u304c\u898b\u3064\u304b\u3089\u306a\u3051\u308c\u3070 \u201cprocess PID not found\u201d \u3068\u3044\u3046\u30e1\u30c3\u30bb\u30fc\u30b8\u3068\u5171\u306bpsutil.NoSuchProcess\u3068\u3044\u3046\u4f8b\u5916\u304c\u6295\u3052\u3089\u308c\u308b\u306e\u3067try-except\u3067\u56f2\u3093\u3067\u304a\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u7d9a\u3051\u3066\u53d6\u5f97\u3057\u305f\u30d7\u30ed\u30bb\u30b9\u306e\u5b50\u30d7\u30ed\u30bb\u30b9\u306e\u30ea\u30b9\u30c8\u3092 children() \u3067\u53d6\u5f97\u3057\u307e\u3059\u3002recursive=True\u3067\u5168\u3066\u306e\u5b50\u5b6b\u30d7\u30ed\u30bb\u30b9\u304c\u8fd4\u3055\u308c\u307e\u3059\u3002\u30ea\u30b9\u30c8\u306b\u89aa\u3092\u8ffd\u52a0\u3057\u3001\u5b50\u304b\u3089\u9806\u306bkill()\u3057\u307e\u3059\u3002\u5ff5\u306e\u305f\u3081kill()\u3082try-except\u3067\u56f2\u3093\u3067\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u306b\u3088\u308b\u3068psutil\u306ekill()\u306fUNIX\u3067\u306fos.kill(pid, signal.SIGKILL)\u3092\u5b9f\u884c\u3057\u3001Windows\u3067\u306fTerminateProcess\u304c\u4f7f\u7528\u3055\u308c\u308b\u3088\u3046\u3067\u3059\u3002psutil\u306eterminate()\u306fUNIX\u3067\u306fos.kill(pid, signal.SIGTERM)\u304c\u5b9f\u884c\u3055\u308c\u307e\u3059\u304c\u3001Windows\u3067\u306fkill()\u306e\u30a8\u30a4\u30ea\u30a2\u30b9\u306e\u3088\u3046\u306a\u306e\u3067Windows\u306e\u5834\u5408\u306fkill()\u3067\u3082terminate()\u3067\u3082\u3069\u3061\u3089\u3067\u3082\u826f\u3055\u305d\u3046\u3067\u3059\u3002<\/p>\n\n\n\n<p>psutil.wait_procs()\u306b\u30d7\u30ed\u30bb\u30b9\u306e\u30ea\u30b9\u30c8\u3092\u6e21\u3057\u3066\u751f\u304d\u3066\u3044\u308b\u30d7\u30ed\u30bb\u30b9\u3068\u7d42\u4e86\u6e08\u307f\u30d7\u30ed\u30bb\u30b9\u306e\u30ea\u30b9\u30c8\u3092\u53d6\u5f97\u3067\u304d\u307e\u3059\u3002\u307e\u305f\u3001\u5f15\u6570\u306b\u95a2\u6570\u3092\u6e21\u3059\u3053\u3068\u3067\u7d42\u4e86\u6e08\u307f\u306e\u5404\u30d7\u30ed\u30bb\u30b9\u306b\u5bfe\u3057\u3066\u305d\u306e\u95a2\u6570\u304c\u5b9f\u884c\u3055\u308c\u307e\u3059\u3002\u4e0a\u306e\u30b3\u30fc\u30c9\u3067\u306fkilled()\u304c\u7d42\u4e86\u6e08\u307f\u306e\u5404\u30d7\u30ed\u30bb\u30b9\u306b\u5bfe\u3057\u3066\u5b9f\u884c\u3055\u308c\u307e\u3059\u3002wait_procs()\u306b\u306f\u5f15\u6570\u306btimeout\u5024\u3092\u6e21\u3059\u3053\u3068\u3082\u53ef\u80fd\u3067\u3001\u3053\u308c\u306fwait_procs()\u304c\u30ea\u30b9\u30c8\u3092\u8fd4\u3059\u307e\u3067\u306e\u6700\u5927\u6642\u9593\u306e\u6307\u5b9a\u3067\u3059\u3002\u6307\u5b9a\u3057\u306a\u3044\u304btimeout\u5024\u3088\u308a\u3082\u65e9\u304f\u5168\u30d7\u30ed\u30bb\u30b9\u304c\u7d42\u4e86\u3057\u305f\u5834\u5408\u306f\u5373\u5ea7\u306b\u30ea\u30b9\u30c8\u304c\u8fd4\u3055\u308c\u307e\u3059\u3002wait_procs()\u306b\u3088\u308b\u30ea\u30b9\u30c8\u53d6\u5f97\u306e\u4f7f\u3044\u9053\u3068\u3057\u3066\u306f\u3001UNIX\u306e\u5834\u5408\u306b\u6700\u521d\u306fkill()\u3067\u306f\u306a\u304fterminate()\u3057\u3066\u307e\u3060\u751f\u304d\u3066\u3044\u308b\u30d7\u30ed\u30bb\u30b9\u304c\u3042\u308c\u3070kill()\u3059\u308b\u3068\u3044\u3063\u305f\u4f7f\u3044\u65b9\u304c\u3042\u308a\u307e\u3059\u3002\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u306e<a href=\"https:\/\/psutil.readthedocs.io\/en\/latest\/index.html#psutil.wait_procs\" data-type=\"link\" data-id=\"https:\/\/psutil.readthedocs.io\/en\/latest\/index.html#psutil.wait_procs\" target=\"_blank\" rel=\"noreferrer noopener\">wait_procs()\u306e\u9805\u76ee<\/a>\u3067\u3082\u305d\u3093\u306a\u4f8b\u304c\u66f8\u304b\u308c\u3066\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u30b3\u30fc\u30c9\u5168\u4f53<\/h2>\n\n\n\n<p>\u4ee5\u4e0b\u3001main.py\u306e\u30b3\u30fc\u30c9\u5168\u4f53\u3067\u3059\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>import psutil\nimport subprocess\nfrom threading import Timer\n\n\ndef main(cmd, timeout=3):\n    proc = subprocess.Popen(\n        cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT\n    )\n\n    def killed(proc):\n        print(f&quot;process {proc} killed with exit code {proc.returncode}&quot;)\n\n    def kill_process(pid):\n        t.cancel()\n        try:\n            parent = psutil.Process(pid)\n        except psutil.NoSuchProcess as e:\n            print(e)\n            return\n\n        procs = parent.children(recursive=True)\n        procs.append(parent)\n        for p in procs:\n            try:\n                p.kill()\n            except Exception as e:\n                print(e)\n        gone, alive = psutil.wait_procs(procs, callback=killed)\n\n    t = Timer(timeout, kill_process, [proc.pid])\n    t.start()\n\n    for line in iter(proc.stdout.readline, b&quot;&quot;):\n        line = line.rstrip().decode()\n        print(line)\n\n    t.cancel()\n    proc.stdout.close()\n    proc.wait()\n\n\nif __name__ == &quot;__main__&quot;:\n    cmd = &quot;python sub.py&quot;\n    timeout = 3\n    main(cmd, timeout)\n<\/code><\/pre><\/div>\n\n\n\n<p>psutil.Popen()\u3092\u4f7f\u3046\u3068\u82e5\u5e72\u697d\u306b\u306a\u308b\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002\u4ee5\u4e0b\u306e\u3088\u3046\u306bsubprocess\u306b\u306f\u7121\u3044psutil\u306e\u95a2\u6570\u304c\u4f7f\u7528\u53ef\u80fd\u3067\u3001subprocess\u3068\u5171\u901a\u3057\u3066\u3044\u308bsend_signal()\u3001terminate()\u3001kill()\u306fpsutils\u306e\u5b9f\u88c5\u304c\u4f7f\u7528\u3055\u308c\u308b\u3088\u3046\u3067\u3059\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>proc = psutil.Popen(\n    cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT\n)\nprocs = proc.parent().children(recursive=True)<\/code><\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">\u96d1\u611f<\/h2>\n\n\n\n<p>\u3053\u308c\u304c\u6a19\u6e96\u51fa\u529b\u306e\u30ea\u30a2\u30eb\u30bf\u30a4\u30e0\u53d6\u5f97\u6642\u306e\u30bf\u30a4\u30e0\u30a2\u30a6\u30c8\u306e\u30d9\u30b9\u30c8\u306a\u65b9\u6cd5\u304b\u306f\u5206\u304b\u308a\u307e\u305b\u3093\u304c\u3001\u3068\u308a\u3042\u3048\u305a\u5b9f\u88c5\u306f\u3067\u304d\u307e\u3057\u305f\u3002\u3061\u306a\u307f\u306b\u3001\u3053\u306e\u8a18\u4e8b\u306f\u4ee5\u4e0b\u304c\u5143\u30cd\u30bf\u3067\u3059\u3002\u540c\u3058\u3053\u3068\u3067\u60a9\u3093\u3067\u3044\u305f\u306e\u3068\u3001\u3053\u306e\u65b9\u304c\u63a1\u7528\u3057\u305f os.killpg(os.getpgid(p.pid), signal.SIGTERM) \u3068\u3044\u3046kill\u306e\u65b9\u6cd5\u304cUNIX\u9650\u5b9a\u306e\u3084\u308a\u65b9\u306a\u306e\u3067\u79c1\u306fpsutil\u3092\u63a1\u7528\u3057\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n\n\n\n<p><a href=\"https:\/\/stackoverflow.com\/questions\/54770131\/subprocess-popen-real-time-output-with-timeout\" data-type=\"link\" data-id=\"https:\/\/stackoverflow.com\/questions\/54770131\/subprocess-popen-real-time-output-with-timeout\">subprocess popen real time output with timeout - Stack Overflow<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python\u306esubprocess\u3067\u4f55\u304b\u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3059\u308b\u969b\u306b\u30bf\u30a4\u30e0\u30a2\u30a6\u30c8\u51e6\u7406\u3092\u5b9f\u88c5\u3057\u305f\u3044\u3053\u3068\u304c\u3042\u308b\u3068\u601d\u3044\u307e\u3059\u3002subprocess\u306frun()\u3001Popen.communicate()\u3001call()\u3001check_call [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":726,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[],"class_list":["post-1573","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/ryo-fujinone.net\/blog\/wp-json\/wp\/v2\/posts\/1573","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ryo-fujinone.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ryo-fujinone.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ryo-fujinone.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ryo-fujinone.net\/blog\/wp-json\/wp\/v2\/comments?post=1573"}],"version-history":[{"count":28,"href":"https:\/\/ryo-fujinone.net\/blog\/wp-json\/wp\/v2\/posts\/1573\/revisions"}],"predecessor-version":[{"id":1596,"href":"https:\/\/ryo-fujinone.net\/blog\/wp-json\/wp\/v2\/posts\/1573\/revisions\/1596"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ryo-fujinone.net\/blog\/wp-json\/wp\/v2\/media\/726"}],"wp:attachment":[{"href":"https:\/\/ryo-fujinone.net\/blog\/wp-json\/wp\/v2\/media?parent=1573"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ryo-fujinone.net\/blog\/wp-json\/wp\/v2\/categories?post=1573"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ryo-fujinone.net\/blog\/wp-json\/wp\/v2\/tags?post=1573"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}