关于haskell程序的问题 cygwin 中的标准输出缓冲区

发布于 2024-10-12 03:50:44 字数 289 浏览 8 评论 0原文

我有一个简单的 haskell 程序,它会

  1. 用 '\n' 向用户提示一些消息
  2. 等待用户输入
  3. 打印用户的输入

如果我从命令提示符或从命令提示符启动的 cygwin shell 启动程序,那就没问题。

但是,如果我从连接到本地 cygwin 环境的 ssh shell 启动程序,则程序在退出之前不会将任何内容写回到终端。看起来 ssh shell 中 STDOUT 的缓冲区不是行缓冲而是块缓冲。

我不想手动冲水。我该如何解决这个问题?

I have a simple haskell program, which do

  1. prompt some message to user, with '\n'
  2. waiting for user input
  3. print user's input

If I launch the program from a command prompt or from a cygwin shell which is launched from a command prompt, it is ok.

But if I launch the program from ssh shell which is connected to a local cygwin environment, the program don't write anything back to terminal until it exit. It looks like the buffer for STDOUT in ssh shell is not line buffered but block buffered.

I don't want to flush manually. How do I fix the problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

怎会甘心 2024-10-19 03:50:44

据推测,您的 Haskell 程序不是 Cygwin 程序,即它未链接到 Cygwin DLL。 SSH 连接在服务器端分配一个所谓的伪终端 (pty) 设备,Cygwin 使用 Windows 管道实现该设备。非 Cygwin 程序只能看到管道,而它们可能期望与 Windows 控制台窗口对话。缓冲问题是其可能的后果之一。请参阅此帖子以获取进一步的说明和可能的解决方法:http://code.google .com/p/mintty/issues/detail?id=56

Presumably your Haskell program isn't a Cygwin program, i.e. it isn't linked against the Cygwin DLL. SSH connections allocate a so-called pseudo terminal (pty) device on the server side, which Cygwin implements using Windows pipes. Non-Cygwin programs only see the pipes, whereas they might expect to be talking to a Windows console window. Buffering issues are one possible consequence of that. See this thread for further explanation and possible workarounds: http://code.google.com/p/mintty/issues/detail?id=56.

北笙凉宸 2024-10-19 03:50:44

您可以手动将缓冲模式设置为您需要的模式:

import System.IO

main = do
  hSetBuffering stdout LineBuffering
  hSetBuffering stdin LineBuffering

有关更多信息,请参阅 System.IO.hSetBuffering

You can manually set the buffering mode to the one you need:

import System.IO

main = do
  hSetBuffering stdout LineBuffering
  hSetBuffering stdin LineBuffering

For more information see System.IO.hSetBuffering.

最后的乘客 2024-10-19 03:50:44

您没有说明如何在 ssh 下启动程序。

如果您在单个步骤中执行此操作,例如

ssh localhost myprogram

,则可能没有分配终端,这通常会导致程序的输出被块缓冲而不是行缓冲。

您可以通过使用 -t 切换到 ssh 来避免这种情况(请检查您的手册以确保):

ssh -t localhost myprogram

另一方面,如果您将 ssh 作为单独的步骤进行登录启动您的程序

ssh localhost
myprogram

,那么这不太可能是原因。

You don't say how you are launching your program under ssh.

If you are doing it in a single step, e.g.

ssh localhost myprogram

then it is possible that a terminal is not being allocated, which would generally cause your program's output to be block buffered instead of line buffered.

You can avoid this by using the -t switch to ssh (check your manual to be sure):

ssh -t localhost myprogram

If, on the other hand, you are sshing in as a separate step to launching your program

ssh localhost
myprogram

then this is unlikely to be the cause.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文