【发布时间】:2020-07-25 09:24:36
【问题描述】:
我正在尝试调用一个外部进程,该进程写入一个我使用withSystemTempFile 获得的临时文件。进程退出后,我可以cat 该文件的内容,但应用程序本身在尝试使用readFile 读取该文件时失败并出现openFile: resource busy (file is locked) 错误。我在回答this 问题时遵循了建议,并使用了readFile 的惰性版本。这是一个示例代码:
module Main where
import Prelude hiding (readFile)
import System.Process (createProcess, shell)
import System.IO (Handle, hShow)
import System.IO.Temp (withSystemTempFile)
import System.IO.Strict (readFile)
main :: IO ()
main = withSystemTempFile "temp.txt" doSomeStuff
where
doSomeStuff :: FilePath -> Handle -> IO ()
doSomeStuff tempFilePath tempFilePathHandle = do
putStrLn tempFilePath
createProcess $ shell $ "echo \"test\" >> " ++ tempFilePath
getLine -- It's here just to stop program while I check that I can actually "cat" the temp file
-- here I'm able to cat the temp file
contents <- readFile tempFilePath -- here the command fails with /tmp/temp23900-0.txt: openFile: resource busy (file is locked)
putStrLn contents
我仍然对 Haskell 有所了解,所以我希望这不是显而易见的事情,因为我已经没有什么想法了。什么给了?
【问题讨论】: