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

发布于 2024-10-19 06:58:14 字数 884 浏览 1 评论 0原文

我需要在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

§普罗旺斯的薰衣草 2024-10-26 06:58:14

有趣的问题。

一种有效的方法是运行命令 shell,然后通过 stdin 将命令传递给它(示例使用 Python 3,对于 Python 2,您可以跳过decode() 调用) 。请注意,命令 shell 调用设置为抑制除写入 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 的结果进行比较:

>>> 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

后两个调用无法看到第一个调用设置的环境,因为所有 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 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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文