我的程序从网络套接字中读取一行并将其写入光盘。由于行可以非常长,而且字符串的性能很差,所以我开始使用惰性字节字符串。现在看来,Haskell将通过磁盘文件句柄上的hClose,而不会将整个字节字符串实际刷新到磁盘,这样做:
hPut将字节字符串写入文件通常导致openFile: resource busy (file is locked)。
是否有可能在关闭文件之前强制执行计算并等待写入整个字节字符串,这样我就可以确定文件在操作之后实际上已经关闭了吗?
发布于 2012-04-13 09:48:06
由于唯一的其他答案是“使用其他东西”的品种,我正在张贴我自己的解决方案。在hClose之后使用此函数将挂起线程,直到延迟写入完成为止。
waitForLazyIO location = do
t <- liftIO $ try $ openFile location AppendMode
handle t
where
handle (Right v) = hClose v
handle (Left e)
-- Add some sleep if you expect the write operation to be slow.
| isAlreadyInUseError e = waitForLazyIO location
| otherwise = throwError $ show e发布于 2012-04-01 08:21:35
尝试使用带有严格字节字符串的严格i/o,而不是懒惰的i/o和延迟字节字符串。
如果结果证明效率太低,请尝试使用导管或类似的包。
https://stackoverflow.com/questions/9960188
复制相似问题