在python中阻止从stdin读取

2024-03-29 09:59:29 发布

您现在位置: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 '!!!!!!!!'

Tags: 数据pytestimportechotrue示例read
1条回答
网友
1楼 · 发布于 2024-03-29 09:59:29

f.read()块,但如果达到EOF,也返回空字符串。您的示例已中断,因为输入流已关闭并且达到EOF。另外,您很可能希望阅读整行内容,因此readline是合适的。在

相关问题 更多 >