【发布时间】:2017-02-04 22:20:09
【问题描述】:
如何在 python (2.7) 中从标准输入执行阻塞读取操作,暂停进程直到管道中出现一些数据?
read() 的问题在于,在第一次返回后,read() 不再阻塞。示例:
echo 'test test ' | python test.py
# test.py
import sys
while True:
string = sys.stdin.read() # Blocks only for the first time
print '!!!!!!!!'
【问题讨论】:
-
pipe将文本test test发送到脚本并关闭连接 - 您希望下一个read()阅读什么? -
另一个进程(例如父进程)可以写入该进程的标准输入。
-
对我来说,父进程只能写入
echo,不能写入脚本。