在python中,我想创建一个子进程并读写数据到它的stdio。 可以说我有下面的C程序,只是将其输入写入其输出。如何在不阻塞的情况下在python中使用管道?
#include <stdio.h>
int main() {
char c;
for(;;) {
scanf("%c", &c);
printf("%c", c);
}
}
在python中,我应该可以使用这个子进程模块。像这样:
from subprocess import *
pipe = Popen("thing", stdin=PIPE, stdout=PIPE)
pipe.stdin.write("blah blah blah")
text = pipe.stdout.read(4) # text should == "blah"
但是在这种情况下,无限期地读取块的调用。 我该如何做我想达到的目标?