{"id":177195,"date":"2020-04-26T08:34:45","date_gmt":"2020-04-26T00:34:45","guid":{"rendered":"https:\/\/gulass.cn\/?p=177195"},"modified":"2020-04-20T09:36:02","modified_gmt":"2020-04-20T01:36:02","slug":"know-python3-subprocess","status":"publish","type":"post","link":"https:\/\/gulass.cn\/know-python3-subprocess.html","title":{"rendered":"\u6559\u4f60\u8ba4\u8bc6Python3 subprocess"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nsubprocess \u6a21\u5757\u5141\u8bb8\u6211\u4eec\u542f\u52a8\u4e00\u4e2a\u65b0\u8fdb\u7a0b\uff0c\u5e76\u8fde\u63a5\u5230\u5b83\u4eec\u7684\u8f93\u5165\/\u8f93\u51fa\/\u9519\u8bef\u7ba1\u9053\uff0c\u4ece\u800c\u83b7\u53d6\u8fd4\u56de\u503c\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/p>\n

\u4f7f\u7528 subprocess \u6a21\u5757<\/strong><\/div>\n

subprocess \u6a21\u5757\u9996\u5148\u63a8\u8350\u4f7f\u7528\u7684\u662f\u5b83\u7684 run \u65b9\u6cd5\uff0c\u66f4\u9ad8\u7ea7\u7684\u7528\u6cd5\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528 Popen \u63a5\u53e3\u3002<\/p>\n

run \u65b9\u6cd5\u8bed\u6cd5\u683c\u5f0f\u5982\u4e0b\uff1a<\/strong><\/p>\n

subprocess.run(args, *, stdin=None, input=None, stdout=None, stderr=None, capture_output=False, shell=False, cwd=None, timeout=None, check=False, encoding=None, errors=None, text=None, env=None, universal_newlines=None)<\/pre>\n
  • args\uff1a\u8868\u793a\u8981\u6267\u884c\u7684\u547d\u4ee4\u3002\u5fc5\u987b\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u5b57\u7b26\u4e32\u53c2\u6570\u5217\u8868\u3002<\/li>\n
  • stdin\u3001stdout \u548c stderr\uff1a\u5b50\u8fdb\u7a0b\u7684\u6807\u51c6\u8f93\u5165\u3001\u8f93\u51fa\u548c\u9519\u8bef\u3002\u5176\u503c\u53ef\u4ee5\u662f subprocess.PIPE\u3001subprocess.DEVNULL\u3001\u4e00\u4e2a\u5df2\u7ecf\u5b58\u5728\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u3001\u5df2\u7ecf\u6253\u5f00\u7684\u6587\u4ef6\u5bf9\u8c61\u6216\u8005 None\u3002subprocess.PIPE \u8868\u793a\u4e3a\u5b50\u8fdb\u7a0b\u521b\u5efa\u65b0\u7684\u7ba1\u9053\u3002subprocess.DEVNULL \u8868\u793a\u4f7f\u7528 os.devnull\u3002\u9ed8\u8ba4\u4f7f\u7528\u7684\u662f None\uff0c\u8868\u793a\u4ec0\u4e48\u90fd\u4e0d\u505a\u3002\u53e6\u5916\uff0cstderr \u53ef\u4ee5\u5408\u5e76\u5230 stdout \u91cc\u4e00\u8d77\u8f93\u51fa\u3002<\/li>\n
  • timeout\uff1a\u8bbe\u7f6e\u547d\u4ee4\u8d85\u65f6\u65f6\u95f4\u3002\u5982\u679c\u547d\u4ee4\u6267\u884c\u65f6\u95f4\u8d85\u65f6\uff0c\u5b50\u8fdb\u7a0b\u5c06\u88ab\u6740\u6b7b\uff0c\u5e76\u5f39\u51fa TimeoutExpired \u5f02\u5e38\u3002<\/li>\n
  • check\uff1a\u5982\u679c\u8be5\u53c2\u6570\u8bbe\u7f6e\u4e3a True\uff0c\u5e76\u4e14\u8fdb\u7a0b\u9000\u51fa\u72b6\u6001\u7801\u4e0d\u662f 0\uff0c\u5219\u5f39 \u51fa CalledProcessError \u5f02\u5e38\u3002<\/li>\n
  • encoding: \u5982\u679c\u6307\u5b9a\u4e86\u8be5\u53c2\u6570\uff0c\u5219 stdin\u3001stdout \u548c stderr \u53ef\u4ee5\u63a5\u6536\u5b57\u7b26\u4e32\u6570\u636e\uff0c\u5e76\u4ee5\u8be5\u7f16\u7801\u65b9\u5f0f\u7f16\u7801\u3002\u5426\u5219\u53ea\u63a5\u6536 bytes \u7c7b\u578b\u7684\u6570\u636e\u3002<\/li>\n
  • shell\uff1a\u5982\u679c\u8be5\u53c2\u6570\u4e3a True\uff0c\u5c06\u901a\u8fc7\u64cd\u4f5c\u7cfb\u7edf\u7684 shell \u6267\u884c\u6307\u5b9a\u7684\u547d\u4ee4\u3002<\/li>\n

    run \u65b9\u6cd5\u8c03\u7528\u65b9\u5f0f\u8fd4\u56de CompletedProcess \u5b9e\u4f8b\uff0c\u548c\u76f4\u63a5 Popen \u5dee\u4e0d\u591a\uff0c\u5b9e\u73b0\u662f\u4e00\u6837\u7684\uff0c\u5b9e\u9645\u4e5f\u662f\u8c03\u7528 Popen\uff0c\u4e0e Popen \u6784\u9020\u51fd\u6570\u5927\u81f4\u76f8\u540c\uff0c\u4f8b\u5982:<\/p>\n

    \u5b9e\u4f8b<\/strong><\/p>\n

    #\u6267\u884cls -l \/dev\/null \u547d\u4ee4\r\n>>> subprocess.run([\"ls\", \"-l\", \"\/dev\/null\"])\r\ncrw-rw-rw-  1 root  wheel    3,   2  5  4 13:34 \/dev\/null\r\nCompletedProcess(args=['ls', '-l', '\/dev\/null'], returncode=0)<\/pre>\n

    returncode<\/strong>: \u6267\u884c\u5b8c\u5b50\u8fdb\u7a0b\u72b6\u6001\uff0c\u901a\u5e38\u8fd4\u56de\u72b6\u6001\u4e3a0\u5219\u8868\u660e\u5b83\u5df2\u7ecf\u8fd0\u884c\u5b8c\u6bd5\uff0c\u82e5\u503c\u4e3a\u8d1f\u503c \"-N\",\u8868\u660e\u5b50\u8fdb\u7a0b\u88ab\u7ec8\u3002<\/p>\n

    \u7b80\u5355\u5b9e\u4f8b\uff1a<\/p>\n

    \u5b9e\u4f8b<\/strong><\/p>\n

    import subprocess\r\ndef runcmd(command):\r\n    ret = subprocess.run(command,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE,encoding=\"utf-8\",timeout=1)\r\n    if ret.returncode == 0:\r\n        print(\"success:\",ret)\r\n    else:\r\n        print(\"error:\",ret)\r\nruncmd([\"dir\",\"\/b\"])#\u5e8f\u5217\u53c2\u6570\r\nruncmd(\"exit 1\")#\u5b57\u7b26\u4e32\u53c2\u6570<\/pre>\n

    \u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n

    success: CompletedProcess(args=['dir', '\/b'], returncode=0, stdout='test.py\\n', stderr='')\r\nerror: CompletedProcess(args='exit 1', returncode=1, stdout='', stderr='')<\/pre>\n
    Popen() \u65b9\u6cd5<\/strong><\/div>\n

    Popen \u662f subprocess\u7684\u6838\u5fc3\uff0c\u5b50\u8fdb\u7a0b\u7684\u521b\u5efa\u548c\u7ba1\u7406\u90fd\u9760\u5b83\u5904\u7406\u3002<\/p>\n

    \u6784\u9020\u51fd\u6570\uff1a<\/strong><\/p>\n

    class subprocess.Popen(args, bufsize=-1, executable=None, stdin=None, stdout=None, stderr=None, \r\npreexec_fn=None, close_fds=True, shell=False, cwd=None, env=None, universal_newlines=False, \r\nstartupinfo=None, creationflags=0,restore_signals=True, start_new_session=False, pass_fds=(),\r\n*, encoding=None, errors=None)<\/pre>\n

    \u5e38\u7528\u53c2\u6570\uff1a<\/strong><\/p>\n

  • args\uff1ashell\u547d\u4ee4\uff0c\u53ef\u4ee5\u662f\u5b57\u7b26\u4e32\u6216\u8005\u5e8f\u5217\u7c7b\u578b\uff08\u5982\uff1alist\uff0c\u5143\u7ec4\uff09<\/li>\n
  • bufsize\uff1a\u7f13\u51b2\u533a\u5927\u5c0f\u3002\u5f53\u521b\u5efa\u6807\u51c6\u6d41\u7684\u7ba1\u9053\u5bf9\u8c61\u65f6\u4f7f\u7528\uff0c\u9ed8\u8ba4-1\u3002
    \n0\uff1a\u4e0d\u4f7f\u7528\u7f13\u51b2\u533a
    \n1\uff1a\u8868\u793a\u884c\u7f13\u51b2\uff0c\u4ec5\u5f53universal_newlines=True\u65f6\u53ef\u7528\uff0c\u4e5f\u5c31\u662f\u6587\u672c\u6a21\u5f0f
    \n\u6b63\u6570\uff1a\u8868\u793a\u7f13\u51b2\u533a\u5927\u5c0f
    \n\u8d1f\u6570\uff1a\u8868\u793a\u4f7f\u7528\u7cfb\u7edf\u9ed8\u8ba4\u7684\u7f13\u51b2\u533a\u5927\u5c0f\u3002<\/li>\n
  • stdin, stdout, stderr\uff1a\u5206\u522b\u8868\u793a\u7a0b\u5e8f\u7684\u6807\u51c6\u8f93\u5165\u3001\u8f93\u51fa\u3001\u9519\u8bef\u53e5\u67c4<\/li>\n
  • preexec_fn\uff1a\u53ea\u5728 Unix \u5e73\u53f0\u4e0b\u6709\u6548\uff0c\u7528\u4e8e\u6307\u5b9a\u4e00\u4e2a\u53ef\u6267\u884c\u5bf9\u8c61\uff08callable object\uff09\uff0c\u5b83\u5c06\u5728\u5b50\u8fdb\u7a0b\u8fd0\u884c\u4e4b\u524d\u88ab\u8c03\u7528<\/li>\n
  • shell\uff1a\u5982\u679c\u8be5\u53c2\u6570\u4e3a True\uff0c\u5c06\u901a\u8fc7\u64cd\u4f5c\u7cfb\u7edf\u7684 shell \u6267\u884c\u6307\u5b9a\u7684\u547d\u4ee4\u3002<\/li>\n
  • cwd\uff1a\u7528\u4e8e\u8bbe\u7f6e\u5b50\u8fdb\u7a0b\u7684\u5f53\u524d\u76ee\u5f55\u3002<\/li>\n
  • env\uff1a\u7528\u4e8e\u6307\u5b9a\u5b50\u8fdb\u7a0b\u7684\u73af\u5883\u53d8\u91cf\u3002\u5982\u679c env = None\uff0c\u5b50\u8fdb\u7a0b\u7684\u73af\u5883\u53d8\u91cf\u5c06\u4ece\u7236\u8fdb\u7a0b\u4e2d\u7ee7\u627f\u3002<\/li>\n

    \u521b\u5efa\u4e00\u4e2a\u5b50\u8fdb\u7a0b\uff0c\u7136\u540e\u6267\u884c\u4e00\u4e2a\u7b80\u5355\u7684\u547d\u4ee4:<\/p>\n

    \u5b9e\u4f8b<\/strong><\/p>\n

    >>> import subprocess\r\n>>> p = subprocess.Popen('ls -l', shell=True)\r\n>>> total 164\r\n-rw-r--r--  1 root root   133 Jul  4 16:25 admin-openrc.sh\r\n-rw-r--r--  1 root root   268 Jul 10 15:55 admin-openrc-v3.sh\r\n...\r\n>>> p.returncode\r\n>>> p.wait()\r\n0\r\n>>> p.returncode<\/pre>\n

    \u8fd9\u91cc\u4e5f\u53ef\u4ee5\u4f7f\u7528 p = subprocess.Popen(['ls', '-cl']) \u6765\u521b\u5efa\u5b50\u8fdb\u7a0b\u3002<\/p>\n

    Popen \u5bf9\u8c61\u65b9\u6cd5<\/strong><\/div>\n
  • poll(): \u68c0\u67e5\u8fdb\u7a0b\u662f\u5426\u7ec8\u6b62\uff0c\u5982\u679c\u7ec8\u6b62\u8fd4\u56de returncode\uff0c\u5426\u5219\u8fd4\u56de None\u3002<\/li>\n
  • wait(timeout): \u7b49\u5f85\u5b50\u8fdb\u7a0b\u7ec8\u6b62\u3002<\/li>\n
  • communicate(input,timeout): \u548c\u5b50\u8fdb\u7a0b\u4ea4\u4e92\uff0c\u53d1\u9001\u548c\u8bfb\u53d6\u6570\u636e\u3002<\/li>\n
  • send_signal(singnal): \u53d1\u9001\u4fe1\u53f7\u5230\u5b50\u8fdb\u7a0b\u3002<\/li>\n
  • terminate(): \u505c\u6b62\u5b50\u8fdb\u7a0b,\u4e5f\u5c31\u662f\u53d1\u9001SIGTERM\u4fe1\u53f7\u5230\u5b50\u8fdb\u7a0b\u3002<\/li>\n
  • kill(): \u6740\u6b7b\u5b50\u8fdb\u7a0b\u3002\u53d1\u9001 SIGKILL \u4fe1\u53f7\u5230\u5b50\u8fdb\u7a0b\u3002<\/li>\n

    \u5b9e\u4f8b<\/strong><\/p>\n

    import time\r\nimport subprocess\r\n\r\ndef cmd(command):\r\n    subp = subprocess.Popen(command,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE,encoding=\"utf-8\")\r\n    subp.wait(2)\r\n    if subp.poll() == 0:\r\n        print(subp.communicate()[1])\r\n    else:\r\n        print(\"\u5931\u8d25\")\r\ncmd(\"java -version\")\r\ncmd(\"exit 1\")<\/pre>\n

    \u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n

    java version \"1.8.0_31\"\r\nJava(TM) SE Runtime Environment (build 1.8.0_31-b13)\r\nJava HotSpot(TM) 64-Bit Server VM (build 25.31-b07, mixed mode)<\/pre>\n

    \u5931\u8d25<\/p>\n","protected":false},"excerpt":{"rendered":"

    subprocess \u6a21\u5757\u9996\u5148\u63a8\u8350\u4f7f\u7528\u7684\u662f\u5b83\u7684 run \u65b9\u6cd5\uff0c\u66f4\u9ad8\u7ea7\u7684\u7528\u6cd5\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528 Popen \u63a5\u53e3\u3002 r […]<\/p>\n","protected":false},"author":1481,"featured_media":177197,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-177195","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/177195","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/users\/1481"}],"replies":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/comments?post=177195"}],"version-history":[{"count":5,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/177195\/revisions"}],"predecessor-version":[{"id":177203,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/177195\/revisions\/177203"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media\/177197"}],"wp:attachment":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media?parent=177195"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/categories?post=177195"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/tags?post=177195"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}