什么STDOUT.sync = true意味着什么? [英] What STDOUT.sync = true means?

查看:118
本文介绍了什么STDOUT.sync = true意味着什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读上帝的源代码 Ruby中的流程监控框架,发现这个 STDOUT.sync = true 。我以前从未见过这样的东西。请解释它的作用,这一行的重点是什么?

I am reading source code for god A process monitoring framework in Ruby and found this STDOUT.sync = true. I've never seen something like this before. Please explain what it does, what the point of this line?

提前致谢。

推荐答案

通常 puts 不会立即写入 STDOUT ,但会在内部缓冲字符串并写入输出更大的块。这样做是因为IO操作很慢,并且通常避免将每个字符立即写入控制台更有意义。

Normally puts does not write immediately to STDOUT, but buffers the strings internally and writes the output in bigger chunks. This is done because IO operations are slow and usually it makes more sense to avoid writing every single character immediately to the console.

此行为会在某些情况下导致问题。想象一下,你想要建立一个进度条(运行一个循环,在广泛的计算之间输出单点)。通过缓冲,结果可能是一段时间没有任何输出,然后突然多次打印出多个点。

This behavior leads to problems in certain situations. Imagine you want to build a progress bar (run a loop that outputs single dots between extensive calculations). With buffering the result might be that there isn't any output for a while and then suddenly multiple dots are printed out at once.

为了避免这种行为而是立即写入到 STDOUT 您可以将 STDOUT 设置为同步模式,如下所示:

To avoid this behavior and instead write immediately to STDOUT you can set STDOUT into sync mode like this:

STDOUT.sync = true

来自 docs


当sync模式为true时,所有输出立即刷新到底层操作系统,并且不在内部缓冲。

When sync mode is true, all output is immediately flushed to the underlying operating system and is not buffered internally.

这篇关于什么STDOUT.sync = true意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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