python 标准输出冲洗和三通
以下代码在通过管道传输到 tee 时以损坏的管道结束,但在未通过管道传输时行为正常:
#!/usr/bin/python
import sys
def testfun():
while 1:
try :
s = sys.stdin.readline()
except(KeyboardInterrupt) :
print('Ctrl-C pressed')
sys.stdout.flush()
return
print s
if __name__ == "__main__":
testfun()
sys.exit()
预期输出:
./bug.py
Ctrl-C pressed
当通过管道传输到 tee 时观察到的内容要么是损坏的管道,要么根本没有输出,即 tee 标准输出上没有任何内容,并且 bug 中没有任何内容.log:
./bug.py | tee bug.log
Traceback (most recent call last):
File "./bug.py", line 14, in <module>
sys.stdout.flush()
IOError: [Errno 32] Broken pipe
这可能是什么原因?
The following code ends with broken pipe when piped into tee, but behave correctly when not piped :
#!/usr/bin/python
import sys
def testfun():
while 1:
try :
s = sys.stdin.readline()
except(KeyboardInterrupt) :
print('Ctrl-C pressed')
sys.stdout.flush()
return
print s
if __name__ == "__main__":
testfun()
sys.exit()
Expected output :
./bug.py
Ctrl-C pressed
What is observed when piped into tee is either a broken pipe or no output at all, ie nothing on tee stdout, and nothing in bug.log :
./bug.py | tee bug.log
Traceback (most recent call last):
File "./bug.py", line 14, in <module>
sys.stdout.flush()
IOError: [Errno 32] Broken pipe
What can be the reason for this ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您按下 Ctrl-C 时,shell 将终止两个进程(
python
和tee
)并拆除连接它们的管道。因此,当您在 python 进程中处理 Ctrl-C 并刷新时,它会发现 tee 已终止并且管道不再存在。 因此出现错误消息。
(顺便说一句,您期望日志中出现任何内容吗?除了退出时刷新之外,我没有看到您的进程输出任何内容)
When you hit Ctrl-C, the shell will terminate both processes (
python
andtee
) and tear down the pipe connecting them.So when you handle the Ctrl-C in your
python
process and flush, it'll find thattee
has been terminated and the pipe is no more. Hence the error message.(As an aside, would you expect anything in the log ? I don't see your process outputting anything other than the flush on exit)
这不是 python 问题,而是 shell 问题,正如 Brian 指出的,按 Ctrl-C 将终止两个进程。 解决方法是使用命名管道:
This is not a python problem but a shell problem, as pointed by Brian, hitting Ctrl-C will terminate both process. Workaround is to use named pipes :
不,按 Ctrl-C 不会终止两个进程。 它仅终止 tee 进程,
tee 进程结束时会关闭脚本和 tee 之间的管道,因此您的脚本会因管道消息损坏而终止。
为了解决这个问题,tee 可以选择将 Ctrl-C 传递到管道中的上一个进程: -i
try: man tee
Nope, hitting Ctrl-C does NOT terminate both processes. It terminates the tee process only,
the end of the tee process close the pipe between your script and tee, and hence your script dies with the broken pipe message.
To fix that, tee has an option to pass the Ctrl-C to its previous process in the pipe: -i
try: man tee