使用 Python 在同一进程中运行三个命令
我需要在 Win32 上运行这三个命令来进行分析/代码覆盖率报告。
vsperfcmd /start:coverage /output:run.coverage
helloclass
vsperfcmd /shutdown
我无法一个一个地运行命令,因为 helloclass 可执行文件应该在 vsperfcmd 的同一进程中进行分析。
我想到的是制作一个批处理文件来运行这三个命令,并在Python中运行该批处理文件。但是,我认为 python 应该有一种方法来执行启动 shell 和运行命令的等效操作。
- 问:如何在 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)
I need to run those three commands for profiling/code coverage reporting on Win32.
vsperfcmd /start:coverage /output:run.coverage
helloclass
vsperfcmd /shutdown
I can't run one command by one because the helloclass executable should be profiled in the same process of vsperfcmd.
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.
- Q : How can I run the commands in the same process in Python?
- Q : Or, how can I launch command shell and run commands in Python?
SOLVED
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有趣的问题。
一种有效的方法是运行命令 shell,然后通过 stdin 将命令传递给它(示例使用 Python 3,对于 Python 2,您可以跳过decode() 调用) 。请注意,命令 shell 调用设置为抑制除写入 stdout 的显式输出之外的所有内容。
将其与单独调用
subprocess.call
的结果进行比较:后两个调用无法看到第一个调用设置的环境,因为所有 3 个调用都是不同的子进程。
Interesting question.
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 thedecode()
call). Note that the command shell invocation is set up to suppress everything except explicit output written to stdout.Compare that to the result of separate invocations of
subprocess.call
:The latter two invocations can't see the environment set up by the first one, as all 3 are distinct child processes.
在 Python 中调用外部命令
如何从 Python 异步运行外部命令?
http://docs.python.org/library/subprocess.html
特别是检查“subprocess”模块并查找“shell”参数。
Calling an external command in Python
How can I run an external command asynchronously from Python?
http://docs.python.org/library/subprocess.html
In particular checkout the 'subprocess' module and lookup the 'shell' parameter.