gpt4 book ai didi

python - subprocess.wait() 的返回码有多少种

转载 作者:可可西里 更新时间:2023-11-01 15:01:37 25 4
gpt4 key购买 nike

我对 python-file 很困惑,它用于将文件从服务器复制到 hadoop。

命令是:hadoop fs -put /localhost/* /hadoop/*代码是:

    cmd = exc_path + ' ' + 'fs -put' + ' ' + src_path + item + ' ' + dst_path
process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
code = process.wait()
logfile.info('type(code) is %s, code is %s\n', type(code), code)

这几天运行正常。但是前天返回了code!=0然后昨天,它运行正常,code == 0 ,然后今天它失败并返回:type(code) is <type 'int'>, code is 255

文档说 wait()应该返回 0 或无,那么为什么我得到 255? hadoop 的 cmd 'put' 应该返回 0(成功时)和 -1(失败时)。

我发现了一些有用的信息: “遗憾的是,当使用 shell=True 运行子进程时,wait() 只会等待 sh 子进程完成,而不是等待命令 cmd。” from

最佳答案

The doc says the wait() should return 0 or None, but why I get a 255.

这是错误的。文档说:

Popen.wait() 等待子进程终止。设置并返回 returncode 属性。

Popen.returncode 子返回码,由 poll() 和 wait() 设置(由 communicate() 间接设置)。 None 值表示进程尚未终止。 负值 -N 表示子进程被信号 N 终止(仅限 Unix)。

如果命令以非零退出代码退出,那么您将得到它。

And the cmd 'put' of hadoop should return 0(when success) and -1(when fail).

关于为什么得到 255 而不是 -1 的解释非常简单并且已经解释过了 herehere .基本上这是由于 Java 允许有符号的 32 位值作为退出代码(对我们来说为 -1),但 Posix 退出状态是无符号的 8 位值。

总而言之,非零退出代码将告诉您命令失败。如果要检查特殊的退出代码,当代码不在 0-255 范围内时必须特别小心。

关于python - subprocess.wait() 的返回码有多少种,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15564850/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com