我遇到了一个非常奇怪的问题,当通过 Kubernetes 运行时,我似乎没有从 Haskell 应用程序获得任何 stdout/stderr 输出。
我正在使用非常基本的putStrLn
来写入标准输出。
如果我在 Kubernetes 环境中手动输入容器并通过 shell 运行应用程序 - 我会看到预期的输出。
此问题的原因可能是什么/
这似乎是由于输出到 kubernetes 日志记录时将输出缓冲设置为"块"模式引起的。
通过将缓冲设置为LineBuffering
来修复:
import System.IO
...
hSetBuffering stdout LineBuffering
hSetBuffering stderr LineBuffering
http://hackage.haskell.org/package/base-4.12.0.0/docs/System-IO.html#t:BufferMode
打开句柄时的默认缓冲模式为 依赖于实现,可能依赖于文件系统对象 连接到该手柄。对于大多数实现,物理 文件通常是块缓冲的,终端通常是 行缓冲。
感谢 fp 松弛的杰西·肯普夫指出我这一点!