[英]Blocking read from stdin in python
如何从 python (2.7) 中的 stdin 执行阻塞读取操作,暂停进程直到管道中出现一些数据?
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 '!!!!!!!!'
f.read()
阻塞,但如果达到 EOF 也会返回一个空字符串。 您的示例已损坏,因为输入流已关闭并已达到 EOF。 此外,您很可能想阅读整行,因此readline
是合适的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.