0

问题是链接中回答的内容的扩展。在尝试使用它延迟打印输出时,cat file_name在 shell 上使用after. 这是代码:

proc foo {times} { 
    while {$times >0} {
        puts "hello$times"
        incr times -1
        after 20000
        puts "hello world" 
    }
}
proc reopenStdout {file} {
    close stdout
    open $file w        ;# The standard channels are special
}

reopenStdout ./bar
foo 10
4

1 回答 1

0

您正在写入的数据正在内存中缓冲,而您写入的数据不足以将内部缓冲区刷新到磁盘。flush stdout在循环中添加一个foo,或者执行一些操作,例如将新打开的通道设置为行缓冲:

proc reopenStdout {file} {
    close stdout
    set ch [open $file w]        ;# The standard channels are special
    chan configure $ch -buffering line
}

如果行缓冲不够,您可以使用chan configure's-buffering和options 来获得最适合您需要的行为。-buffersize

于 2021-09-09T08:08:52.017 回答