如何让这两个进程(程序)使用管道直接相互通信?

发布于 2024-10-06 22:19:12 字数 331 浏览 0 评论 0原文

程序A是一个c程序,它不断地接收stdin中的输入,处理它并将其输出到stdout。

我想编写程序 B (用 python ),这样它就会读取 A 的输出,并反馈所需的任何内容。 请注意,每个程序必须只有一个实例,因此给定 b1 和 b2 ,它们是 b 的实例,而不是:

$ b1 | a | b2

我需要

$ b1 | a | b1 

以下是最终所需结果的图表:

替代文本

Program A, is a c program that endlessly, receives input in stdin, process it and output it to stdout.

I want to write program B (in python) so it will reads A's output, and feed it back with whatever is needed.
Note there must be only one instance of each of those programs, so given b1 and b2 which are instances of b instead of:

$ b1 | a | b2

I need to have

$ b1 | a | b1 

The following is the diagram of the final desired result:

alt text

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

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

发布评论

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

评论(2

霞映澄塘 2024-10-13 22:19:12

使用 subprocess.Popen 类来创建程序A的子进程。例如:

import subprocess
import sys

# Create subprocess with pipes for stdin and stdout
progA = subprocess.Popen("a", stdin=subprocess.PIPE, stdout=subprocess.PIPE)

# Reassign the pipes to our stdin and stdout
sys.stdin = progA.stdout
sys.stdout = progA.stdin

现在两个进程可以通过管道相互通信。将原始的 sys.stdin 和 sys.stdout 保存到其他变量中也可能是一个好主意,这样如果您决定终止子进程,就可以恢复 stdin和标准输出到其原始状态(例如终端)。

Use the subprocess.Popen class to create a subprocess for program A. For example:

import subprocess
import sys

# Create subprocess with pipes for stdin and stdout
progA = subprocess.Popen("a", stdin=subprocess.PIPE, stdout=subprocess.PIPE)

# Reassign the pipes to our stdin and stdout
sys.stdin = progA.stdout
sys.stdout = progA.stdin

Now the two processes can communicate back and forth with each other via the pipes. It might also be a good idea to save the original sys.stdin and sys.stdout into other variables so that if you ever decide to terminate the subprocess, you can restore stdin and stdout to their original states (e.g. the terminal).

以为你会在 2024-10-13 22:19:12

对于那些通过 Google 来到这里的人:实际上有一个非常好的使用命名管道的方法:

首先创建 2 个命名管道:

mkfifo pipe1
mkfifo pipe2

然后运行这个:

echo -n x | cat - pipe1 > pipe2 & cat <pipe2 > pipe1

这将导致 cat 命令始终相互复制字母 x。所以现在您可以随意使用自己的程序而不是 cat 来处理输入和输出。这不限于Python。您还可以将 java 程序与 c++ 程序连接起来。

例如,如果您的程序名为 A.py 和 B.py,则初始命令将是:

echo -n x | ./A.py - pipe1 > pipe2 & ./B.py <pipe2 > pipe1

For those who come here by Google: there is actually a very good way using named pipes:

first create 2 names pipes:

mkfifo pipe1
mkfifo pipe2

then run this:

echo -n x | cat - pipe1 > pipe2 & cat <pipe2 > pipe1

this will cause the cat commands to copy the letter x all the time to each other. So now you can feel free to use your own programs instead of cat, to process the input and output. This is not limited to python. You can also connect a java program with a c++ program.

For example if your programs are called A.py and B.py the initial command would be:

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