使用Python在同一过程中运行三个命令

问题描述:

我需要在Win32上运行这三个命令来进行性能分析/代码覆盖率报告.

I need to run those three commands for profiling/code coverage reporting on Win32.

vsperfcmd /start:coverage /output:run.coverage
helloclass
vsperfcmd /shutdown

我不能一一执行命令,因为helloclass可执行文件应该在vsperfcmd的同一过程中进行概要分析.

I can't run one command by one because the helloclass executable should be profiled in the same process of vsperfcmd.

我想到的是制作一个批处理文件来运行这三个命令,然后在Python中运行该批处理文件.但是,我认为python应该有一种方法可以执行启动外壳程序和运行命令的等效操作.

What I think of is to make a batch file to run those three commands, and run the batch file in Python. However, I think python should have a way to do the equivalent action of launching a shell and run commands.

  • 问:如何在Python的同一进程中运行命令?
  • 问:或者,如何启动命令shell并在Python中运行命令?
import subprocess
cmdline = ["cmd", "/q", "/k", "echo off"]
cmd = subprocess.Popen(cmdline, stdin=subprocess.PIPE, stdout=subprocess.PIPE, shell=True)
batch = b"""\
rem vsinstr -coverage helloclass.exe /exclude:std::*
vsperfcmd /start:coverage /output:run.coverage
helloclass
vsperfcmd /shutdown
exit
"""
cmd.stdin.write(batch)
cmd.stdin.flush() # Must include this to ensure data is passed to child process
result = cmd.stdout.read()
print(result)

有趣的问题.

一种可行的方法是运行命令外壳,然后通过stdin将命令通过管道传递给它(示例使用Python 3,对于Python 2,您可以跳过decode()调用).请注意,命令外壳程序调用的设置是禁止显示除写入stdout的显式输出外的所有内容.

One approach that works is to run a command shell and then pipe commands to it via stdin (example uses Python 3, for Python 2 you can skip the decode() call). Note that the command shell invocation is set up to suppress everything except explicit output written to stdout.

>>> import subprocess
>>> cmdline = ["cmd", "/q", "/k", "echo off"]
>>> cmd = subprocess.Popen(cmdline, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
>>> batch = b"""\
... set TEST_VAR=Hello World
... set TEST_VAR
... echo %TEST_VAR%
... exit
... """
>>> cmd.stdin.write(batch)
59
>>> cmd.stdin.flush() # Must include this to ensure data is passed to child process
>>> result = cmd.stdout.read()
>>> print(result.decode())
TEST_VAR=Hello World
Hello World

将其与subprocess.call的单独调用的结果进行比较:

Compare that to the result of separate invocations of subprocess.call:

>>> subprocess.call(["set", "TEST_VAR=Hello World"], shell=True)
0
>>> subprocess.call(["set", "TEST_VAR"], shell=True)
Environment variable TEST_VAR not defined
1
>>> subprocess.call(["echo", "%TEST_VAR%"], shell=True)
%TEST_VAR%
0

后两个调用看不到第一个调用所建立的环境,因为这三个调用都是不同的子进程.

The latter two invocations can't see the environment set up by the first one, as all 3 are distinct child processes.