Python子进程PIPE阻塞

时间:2016-02-23 09:36:59

标签: python process pipe subprocess stdout

子进程将输出几个字符,我想通过stdin发送一些响应。输出的长度是可预测的,所以我写了这样的代码:

p = Popen("myproc", shell = True, stdin = PIPE, stdout = PIPE)
p.stdout.read(1)

但是,即使进程输出的字符多于一个字符,p.stdout.read(1)也不会返回。如何在读取预期长度的字节序列后使其停止阻塞?

1 个答案:

答案 0 :(得分:1)

当标准输出被重定向时,

myproc可以使用块缓冲模式,即,在刷新子对应的缓冲区之前,您的父Python不会看到任何内容。请参阅several ways to workaround it in this answer (and follow the corresponding links there)

相关问题