为什么有时 Python 子进程在运行进程后无法获得正确的退出代码?

2022-01-18 00:00:00 python windows subprocess jython sikuli

问题描述

我正在使用 Python 子进程在 Windows 7 上运行外部脚本.我正在尝试获取退出代码.

I am using Python subprocess to run external scripts on Windows 7. I am trying to get the exit code.

在案例 1 中,我运行一个 python 脚本 test1.py.

In case 1, I run a python script test1.py.

test1.py

import sys
sys.exit(24)   <--exit code

myscript1.py

myscript1.py

import subprocess
process = subprocess.Popen(["python", "C:\path\to\test1.py"], stdout=subprocess.PIPE)
process.wait()
print process.returncode

在 Windows 命令提示符下,当我运行脚本时,我得到以下输出:

In Windows command prompt, when I run the script, I get the following output:

>python test1.py
>
>echo %errorlevel%
>24
>
>python myscript1.py
>24

因此,您可以看到在这种情况下子进程能够获得正确的退出代码.

So, you can see that subprocess is able to get the correct exit code in this case.

在案例 2 中,我运行一个批处理文件 test2.cmd.

In case 2, I run a batch file test2.cmd.

test2.cmd

EXIT /B 56   <--exit code

myscript2.py

myscript2.py

import subprocess
process = subprocess.Popen(["C:\path\to\test2.cmd"], stdout=subprocess.PIPE)
process.wait()
print process.returncode

在 Windows 命令提示符下,当我运行脚本时,我得到以下输出:

In Windows command prompt, when I run the script, I get the following output:

>test2.cmd
>
>echo %errorlevel%
>56
>
>python myscript2.py
>56

因此,您可以看到子进程在这种情况下也能够获得正确的退出代码.

So, you can see that subprocess is also able to get the correct exit code in this case.

在案例 3 中,我运行 SikuliX 脚本.

In case 3, I run a SikuliX script.

test3.sikuli

test3.sikuli

xxx xxx (sikuli script here)
xxx xxx
...
exit(16)   <--exit code

myscript3.py

myscript3.py

import subprocess
process = subprocess.Popen(["C:\path\to\runsikuli.cmd", "-r", "C:\path\to\sikuli-script.sikuli"], stdout=subprocess.PIPE)
process.wait()
print process.returncode

在 Windows 命令提示符下,当我运行脚本时,我得到以下输出:

In Windows command prompt, when I run the script, I get the following output:

>C:path	ounsikuli.cmd -r C:path	osikuli-script.sikuli
>... (stdout + stderr)
>16
>
>echo %errorlevel%
>16
>
>python myscript3.py
>0

在情况 3 中,当我在命令提示符下手动运行脚本时,它能够设置 %errorlevel%.当我使用 Python 子进程运行脚本时,子进程无法获取正确的退出代码.它总是返回 0.

In case 3, when I run the script manually in the command prompt, it is able to set the %errorlevel%. When I run the script using Python subprocess, subprocess is unable to get the correct exit code. It always return 0.

案例3为什么Python子进程获取不到退出码?

Why Python subprocess failed to get the exit code in case 3?


解决方案

As 您的评论说如果您运行相同的命令,那么命令提示符和使用 Python subprocess 模块产生相同的结果(0 退出代码).

As your comment says if you run the same command then both the command prompt and using Python subprocess module produce the same result (0 exit code).

您可能会看到不同的退出代码,因为您使用了不同的命令.这里没有惊喜.Python subprocess 模块返回正确的(对于给定命令)退出代码.

You may see different exit codes because you use different commands. No surprise here. Python subprocess module returns correct (for a given command) exit code.

如果它没有返回正确的退出状态,那么你可以添加 &exit 在您的 cmd.exe 命令末尾作为一种解决方法以获得正确的返回码,请参阅 "Windows 上的子进程:Python 问题跟踪器上 shell=True" 的错误返回代码:

If it doesn't return the right exit status then you could add & exit at the end of your cmd.exe command as a workaround to get the correct return code, see "subprocess on Windows: wrong return code with shell=True" on Python issue tracker:

from subprocess import check_call

check_call(r'C:path	ounsikuli.cmd -r C:path	osikuli-script.sikuli & exit',
           shell=True)

相关文章