【发布时间】:2020-07-22 11:43:53
【问题描述】:
所以我有一个使用 System.Process.Typed 库与子进程交互的 Haskell 程序。我试图在子进程的整个生命周期内捕获子进程的标准错误。如果子进程在我到达* 行之前完成,则当前方法不起作用。我认为要做到这一点,我需要使用 STM,但我对 STM 一无所知,所以想知道是否有更简单的方法。
fun :: MyType -> IO MyOtherType
fun inparam = withProcessWait config $ \process -> do
hPutStrLn (getStdin process) (getStr1 inparam)
hFlush (getStdin process)
response1 <- hGetLine (getStdout process)
hPutStrLn (getStdin process) (getStr2 inparam)
hFlush (getStdin process)
response2 <- hGetLine (getStdout process)
err <- hGetContents (getStderr process) -- this is line *
hClose (getStdin process)
exitCode <- timedWaitExitCode 100 process
return $ MyOtherType response1 response2 err
where
config = setStdin createPipe
$ setStdout createPipe
$ setStderr createPipe
$ fromString (fp inparam)
提前谢谢你。
编辑 1:修复了 * 标签
编辑 2:当我尝试运行代码时,我得到 Exception: [..] hGetContents: illegal operation (delayed read on closed handle)
【问题讨论】:
-
能否请您扩展一下“不起作用”?究竟发生了什么,而不是你所期望的?
-
如果子进程在
*行之前完成,则会引发异常,因为句柄会关闭。
标签: haskell subprocess child-process stm io-monad