What STDOUT.sync = true means?

后端 未结 1 479
一整个雨季
一整个雨季 2020-12-10 10:51

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 bef

相关标签:
1条回答
  • 2020-12-10 11:06

    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.

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

    STDOUT.sync = true
    

    From the docs:

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

    0 讨论(0)
提交回复
热议问题