在 Sublime Text 3 中运行 Python 时如何删除输出缓冲 [英] How to remove output buffering when running Python in Sublime Text 3

查看:45
本文介绍了在 Sublime Text 3 中运行 Python 时如何删除输出缓冲的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在构建 Python 3 脚本时从 Sublime Text 3 中删除输出缓冲?我想要实时输出.

How can I remove the output buffering from Sublime Text 3 when I build a Python 3 script? I would like real-time output.

我正在使用带有 Anaconda 插件、Python 3.6 和 Linux Mint 18 的 Sublime Text 3.当我使用 control-b 运行一个简单的脚本时:

I am using Sublime Text 3 with the Anaconda plugin, Python 3.6 and Linux Mint 18. When I run a simple script using control-b:

print('hello')

我在名为构建输出"的单独窗口中获得即时输出.当我使用重复输出的脚本时,例如:

I get an instant output in a separate window called 'Build output'. When I use a script with a repeated output, such as:

from time import sleep

count = 0
print('starting')
while True:
    print('{} hello'.format(count))
    count += 1
    sleep(0.5)

最初我在构建输出"中看到一个空白屏幕.一段时间后,它填充了数百行输出.看起来输出正在被缓冲.当缓冲区已满时,它会立即将所有内容输出到构建输出"屏幕.

Initially I get a blank screen in 'Build output'. Some time later it populates with several hundred lines of output. It looks like the output is being buffered. When the buffer is full, it outputs all at once to the 'Build output' screen.

编辑Sublime Text 允许自定义构建配置.默认的 Python 构建是针对 Python 2 的.我输入了 Python 3 的构建配置,但错过了 -u 标志.解决方法是将 -u 标志放在 Python 3 构建中.

Edit Sublime Text allows custom build configurations. The default Python build is for python 2. I entered a build configuration for Python 3 and missed the -u flag. The fix is to put the -u flag in the Python 3 build.

文件:Python3.sublime-build

File: Python3.sublime-build

{
    "shell_cmd": "/usr/bin/env python3 -u ${file}",
    "selector": "source.python",
    "file_regex": "^(...*?):([0-9]*):?([0-9]*)",
    "working_dir": "${file_path}",
}

保存在 sublime_install/Data/Packages/User/Python3.sublime-build

Save in sublime_install/Data/Packages/User/Python3.sublime-build

推荐答案

默认情况下 exec 命令用于执行构建系统中的命令,exec 命令根本不缓冲输出.这个答案中有更多信息(它还提供了一个执行行缓冲的 exec 版本) 但简而言之 exec 启动一个线程来处理 stdout 和一个处理 stderr,并且两者都将它们获得的任何数据尽快转发到面板当他们得到它时.

By default the exec command is used to execute the commands in build systems, and the exec command doesn't buffer output at all. There is more information in this answer (which also provides a version of exec that does line buffering) but in short exec launches one thread to handle stdout and one to handle stderr, and both forward whatever data they get to the panel as soon as they get it.

因此,像您在此处描述的问题通常是由程序自己进行缓冲引起的.根据您使用的语言和平台,缓冲可能会以意想不到的方式改变您的预期:

As such, a problem like the one you're describing here is generally caused by the program doing it's own buffering. Depending on the language and platform that you're using, buffering may change from what you expect in unexpected ways:

例如,请参阅 stdout 手册页中的此文本 在 Linux 下:

For example, see this text in the man page for stdout under Linux:

流标准错误是无缓冲的.流标准输出在指向终端时是行缓冲的.在调用 fflush(3) 或 exit(3) 或打印换行符之前,不会出现部分行.这会产生意想不到的结果,尤其是调试输出.

The stream stderr is unbuffered. The stream stdout is line-buffered when it points to a terminal. Partial lines will not appear until fflush(3) or exit(3) is called, or a newline is printed. This can produce unexpected results, especially with debugging output.

在一般情况下,此问题的解决方案是修改程序本身以确保它没有缓冲,您将如何做到这一点取决于您使用的语言和您所在的平台.它可以像设置环境变量一样简单,也可以像启动代码一样复杂,以确保无论环境如何缓冲都按照您的预期设置.

In the general case, the solution to this problem would be to modify the program itself to ensure that it's not buffering, and how you would do that depends on the language you're using and the platform that you're on. It could be something as simple as setting an environment variable or as complex as startup code that ensures that regardless of circumstance buffering is set as you expect it to be.

在 Python 的特定情况下,解释器的 -u 命令行参数告诉 Python 保持无缓冲:

In the specific case of Python, the -u command line argument to the interpreter tells Python to keep things unbuffered:

-u     : unbuffered binary stdout and stderr; also PYTHONUNBUFFERED=x
         see man page for details on internal buffering relating to '-u'

Sublime 附带的 Python.sublime-build 将此参数用于 python 命令以确保输出是无缓冲的,并且使用该构建系统按预期工作用于您的示例程序.

The Python.sublime-build that ships with Sublime uses this argument to the python command to ensure that the output is unbuffered, and using that build system works as expected for your sample program.

我不使用 Anaconda 包,所以我不确定它是否提供自己的构建系统,但您可能需要检查您正在使用的构建命令以确保它使用 -你.

I don't use the Anaconda package so I'm not sure if it provides it's own build systems or not, but you may want to check the build command that you're using to ensure that it uses -u.

这篇关于在 Sublime Text 3 中运行 Python 时如何删除输出缓冲的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆