【发布时间】:2010-11-24 08:02:45
【问题描述】:
在python中,我想创建一个子进程并将数据读写到它的stdio。 假设我有以下 C 程序,它只是将其输入写入其输出。
#include <stdio.h>
int main() {
char c;
for(;;) {
scanf("%c", &c);
printf("%c", c);
}
}
在 python 中,我应该能够使用 subprocess 模块来使用它。像这样的:
from subprocess import *
pipe = Popen("thing", stdin=PIPE, stdout=PIPE)
pipe.stdin.write("blah blah blah")
text = pipe.stdout.read(4) # text should == "blah"
但是在这种情况下,读取的调用会无限期地阻塞。 我该如何做我想要实现的目标?
【问题讨论】:
标签: python pipe subprocess popen