这段代码从stdin
获取字符并将它们转换为字符串。使用 hWaitForInput
实现的输入存在超时。我已经包含了 tryIOError
函数来捕获 isEOFError
。当程序运行并且没有发生输入(即正在等待输入)时,它会正确超时。然而,一旦输入一个字符,它就不再超时。无论输入等待时间是多少。
-- get string from stdin
getFrmStdIn :: Int -> IO String
getFrmStdIn timeout = do
inAvail <- tryIOError $ hWaitForInput stdin timeout
let tOut = either (const False) id inAvail
if isLeft inAvail
then return []
else
if not tOut
then die "00 timed out"
else
(:) <$> hGetChar stdin <*> getFrmStdIn timeout
最佳答案
您使用的操作系统和 GHC 版本是什么?
据我所知,最新的 GHC
版本 hWaitForInput
doesn't work on linux当超时不为 0 时根本不会。在 Windows 上,整个 IO 子系统为 "a bit lacking"也是。
关于haskell - hWaitForInput 如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43409494/