您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

如何从subprocess.Popen获取退出代码?

5b51 2022/1/14 8:22:12 python 字数 3113 阅读 518 来源 www.jb51.cc/python

使用下面的代码,p.returncode始终为None.根据 Popen.returncode documentation,这意味着该过程尚未完成. 为什么我没有获得退出代码? import os import sys import subprocess cmd = ['echo','hello'] p = subprocess.Popen(cmd, s

概述

为什么我没有获得退出代码

import os
import sys
import subprocess

cmd = ['echo','hello']
p = subprocess.Popen(cmd,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
try:
    # Filter stdout
    for line in iter(p.stdout.readline,''):
        sys.stdout.flush()
        # Print status
        print(">>> " + line.rstrip())
        sys.stdout.flush()
except:
    sys.stdout.flush()

print 'RETURN CODE',p.returncode

请注意:我之所以单独阅读每一行是因为我想实时过滤其他长期运行的进程的输出,并根据某些字符串暂停它们.

我使用的是Python 2.7.5(CentOS 7 64位).

感谢@skyking发布的答案,我现在可以成功捕获这样的退出代码,使用Popen.poll()(Popen.wait()死锁我的进程):

import os
import sys
import subprocess
import time

cmd = ['echo',''):
        sys.stdout.flush()
        # Print status
        print(">>> " + line.rstrip())
        sys.stdout.flush()
except:
    sys.stdout.flush()

# Wait until process terminates (without using p.wait())
while p.poll() is None:
    # Process hasn't exited yet,let's wait some
    time.sleep(0.5)

# Get return code from process
return_code = p.returncode

print 'RETURN CODE',return_code

# Exit with return code from process
sys.exit(return_code)

The child return code,set by poll() and wait() (and indirectly by
communicate()). A None value indicates that the process hasn’t terminated yet.

A negative value -N indicates that the child was terminated by signal N (Unix only).

您尚未调用poll或wait,因此不会设置returncode.

另一方面,如果你查看fx check_output的源代码,你会发现它们直接使用poll的返回值来检查返回代码.他们知道这个过程已经终止,因为他们已经提前等待了.如果您不知道必须调用wait方法(但请注意文档中记录的死锁可能性).

通常情况下,当你读完stdout / stderr时,程序会终止,但这不能保证,这可能就是你所看到的.程序或操作系统可以在进程实际终止之前关闭stdout(和stderr),然后在读完程序的所有输出后立即调用poll可能会失败.

总结

以上是编程之家为你收集整理的如何从subprocess.Popen获取退出代码?全部内容,希望文章能够帮你解决如何从subprocess.Popen获取退出代码?所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶