Reading from a handle obtained outside createProcess

允我心安 提交于 2020-01-04 04:10:15

问题


I'm trying to create a process, and communicate with it via a handle that I provide outside the createProcess function:

stdOutH <- openFile (logDir </> "stdout.log") ReadWriteMode
hSetBuffering stdOutH LineBuffering
(_, _, _, ph) <- createProcess $
    (proc "someproc" []) { std_out = UseHandle stdOutH
                         , std_err = UseHandle stdErrH
                         }
line <- hGetLine stdOutH
putStrLn $ "Got " ++ line

The "someproc" process spits a line out to the standard output, and I want to read it from the process that spawned it. However if I try to do this I get the following error:

hGetLine: illegal operation (handle is closed)

What I don't understand is why the handle is closed while the created process is running. This works if I use CreatePipe instead of UseHandle, the problem is that I only want to read the first line. But doing this requires to keep on reading from the pipe, otherwise it gets full after a certain amount of output by "someproc".

So, is there a way to use system.process to communicate two processes via stdOutH in the way described above?


回答1:


This behavior of createProcess is documented:

Note that Handles provided for std_in, std_out, or std_err via the UseHandle constructor will be closed by calling this function.

Documentation suggests to use createProcess_ function instead.



来源:https://stackoverflow.com/questions/47053799/reading-from-a-handle-obtained-outside-createprocess

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!