从python/子进程调用另一个进程需要访问shell

发布于 2024-08-30 09:22:19 字数 266 浏览 4 评论 0原文

我正在使用 Popen 从 python 调用 C/C++ 程序,python 代码应该观察子进程的行为并为他自己的工作收集一些数据。

问题是 C 代码已经使用管道来调用一些 shell 命令 - 所以在我从 python 执行之后,C 程序无法执行 bash shell 命令。

有没有办法从 Popen 调用来指定子进程应该在 shell 中执行自己的管道命令???

我尝试使用 shell=True,但没有帮助!

I'm calling a C/C++ program from python with Popen, python code should observe behavior of child process and collect some data for his own work.

Problem is that C code already uses pipes for calling some shell commands - so after my execution from python, C program cannot execute bash shell command.

Is there any way in calling from Popen to specify that, child process should execute his own pipe command in shell???

I tried with shell=True, but doesn't help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

半岛未凉 2024-09-06 09:22:19

从您的描述来看,您似乎正在使用 Popen 执行命令行,其中 Popen 调用 shell。

您应该让 Popen 直接调用您的程序,如下所示:

from subprocess import Popen,PIPE
p = Popen(['/my/fancy/program','-myarg1','-myarg2'],stdout=PIPE)
from_process = p.communicate()[1]

From your description, it seems that you are using Popen to execute a command line, which has Popen call a shell.

You should instead have Popen call your program directly, like this:

from subprocess import Popen,PIPE
p = Popen(['/my/fancy/program','-myarg1','-myarg2'],stdout=PIPE)
from_process = p.communicate()[1]
清晰传感 2024-09-06 09:22:19

最好的方法可能是使用 TCP 连接到本地主机。如果您使用的是 *nix,您可以通过打开一个临时文件并从主机应用程序轮询它来完成此操作。

The best way is probably to use a TCP connection to localhost. If you are using a *nix, you can probably do it by opening a temporary file and polling it from the host application.

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