使用 SSH 时,Python 脚本的输出仅在终止时显示?
我正在运行一个脚本来管理远程(SSH)计算机上的进程。让我们称之为 Five.py
#!/usr/bin/python
import time, subprocess
subprocess.call('echo 0',shell=True)
for i in range(1,5):
time.sleep(1)
print(i)
如果我现在运行,
ssh user@host five.py
我希望看到输出
0
1
2
3
4
每秒出现在我的标准输出上(就像在本地执行一样)。发生的情况是:我得到 0立即从“echo”开始,其余部分仅在整个程序完成后立即出现。 (无助于将 ' Five.py' 嵌套到 bash 脚本中;通过 'python Five.py' 调用它;或者使用 'print >> sys.stdout, i')。
这必须与 python 写入 stdout 的方式有关,因为其他程序的行为非常正常。一个功能性的解决方法是
import time, subprocess
import sys
subprocess.call('echo 0',shell=True)
for i in range(1,5):
time.sleep(1)
sys.stdout.write(str(i)+'\n')
sys.stdout.flush()
但是必须有一个比更改我所有打印语句更好的解决方案!
I'm running a script to manage processes on a remote (SSH) machine. Let's call it five.py
#!/usr/bin/python
import time, subprocess
subprocess.call('echo 0',shell=True)
for i in range(1,5):
time.sleep(1)
print(i)
If i now run
ssh user@host five.py
I would like to see the output
0
1
2
3
4
appear on my standard out second by second (as it does if execute locally).. What happens is: I get the 0 from "echo" right away and the rest only appears at once after the entire program finishes. (Doesn't help to nest 'five.py' into a bash script; to call it by 'python five.py'; or to use 'print >> sys.stdout, i').
This must be related to the way python writes to stdout, since other programs behave quite normal.. A functional workaround is
import time, subprocess
import sys
subprocess.call('echo 0',shell=True)
for i in range(1,5):
time.sleep(1)
sys.stdout.write(str(i)+'\n')
sys.stdout.flush()
But there must be a better solution than changing all my print statements!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以按照 interjay 的提示在 shebang 行上添加 -u
您还可以在关闭缓冲的情况下重新打开 stdout 或设置为行缓冲
通常行缓冲是一个不错的选择
You can add the -u on the shebang line as interjay hinted
You could also reopen stdout with buffering turned off or set to line buffering
Usually line buffering is a good choice
您可以替换 sys.stdout 对象,以便它在每次写入后自动刷新。这也会影响
print
语句。一个例子,取自这个答案:编辑:另一种选择是使用
-u
< 启动 Python /a> 选项,这将强制输入和输出不被缓冲。You can replace the
sys.stdout
object so that it automatically flushes after each write. This will also affect theprint
statement. An example, taken from this answer:Edit: Another option is to start Python with the
-u
option, which will force input and output to be unbuffered.由于您已经在使用 Python,因此您可能会考虑使用的一件事是 Paramiko,这是更好的方法远程 SSH 工作。这是一篇关于我如何我正在以其最基本的形式使用它。
One thing you might look at using since you are already using Python is Paramiko, much nicer way to do remote SSH work. Here is an article about how I am using it in its most basic form.