STDOUT.sync = true 是什么意思? [英] What STDOUT.sync = true means?

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

问题描述

我正在阅读god 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 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

来自文档:

当同步模式为真时,所有输出都会立即刷新到底层操作系统,并且不会在内部进行缓冲.

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天全站免登陆