python - 在 komodo 编辑中使用 time.sleep()?

标签 python python-3.x time komodo komodoedit

我正在尝试使用 python 在 komodo edit 中制作一个简单的程序,当它运行时将在命令输出中打印出 10 秒的时间。

代码如下:

import time

seconds = 0

while seconds != 10:
    time.sleep(1)
    seconds += 1
    print(">", seconds)

当我在 komodo edit 中运行它时,它没有打印出想要的数字。

我希望在一秒后打印数字 1,在两秒后打印数字 2,等等。

相反,它会在 10 秒后打印出所有数字 (1-10)。

我在 python IDLE 中运行了这个完全相同的程序,它正常工作,每秒打印一个数字。

我做错了什么或者我不理解/不知道什么?

最佳答案

该程序可能在其认为其输出未连接到终端的环境中运行,因此 stdout 默认为 block 缓冲,而不是行缓冲。由于您输出的数据非常少,因此缓冲区永远不会填满,并且只会在程序退出之前刷新。

最简单的修复方法是将 flush=True 参数添加到您的 print 调用中,因此缓冲区会在每次 print 后显式刷新:

print(">", seconds, flush=True)

关于python - 在 komodo 编辑中使用 time.sleep()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51716809/

相关文章:

python - 如何使用 Python 更改基于 excel 中文本的颜色?

python-3.x - tensorflow (使用 Keras)中出现 'InvalidArgumentError: Incompatible shapes: [10,2] vs. [10]' 的原因是什么?

javascript - 更新 moment.js 持续时间

c - 使用 difftime 获取从现在到 future 的时差(以秒为单位)

python - 为什么 Pandas 数据帧 to_dict ("records") 与另一个简单的实现相比性能很差?

python - Python 作用域规则是否符合词法作用域的定义?

Python 类到 Yaml/JSON

python-3.x - 超出 Google Colaboratory : Import Data, 堆栈大小?

python - 使 Int64 成为默认的整数 dtype,而不是 pandas 中的标准 int64

time - Go 的时间到底有多精确?