[Haskell-cafe] Standard output of exec
Michael Snoyman
michael at snoyman.com
Thu Dec 11 16:44:04 UTC 2014
That's not going to be sufficient; you could end up in a deadlock by not
flushing the output buffer of the child process. Have a look at the
implementation of readProcess to see how to deal with some of these tricky
issues in the lazy I/O world:
http://hackage.haskell.org/package/process-1.2.0.0/docs/src/System-Process.html#readProcess
On Thu Dec 11 2014 at 6:40:54 PM Derek McLoughlin <
derek.mcloughlin at gmail.com> wrote:
> Something like this would work:
>
> import System.Process
> import System.IO
> import System.Exit
>
> readExecOutput :: String -> [String] -> IO ()
> readExecOutput cmdName cmdOptions = do
> (_, Just hOut, _, jHandle) <-
> createProcess (proc cmdName cmdOptions)
> { cwd = Just "."
> , std_out = CreatePipe
> }
> exitCode <- waitForProcess jHandle
> exec_output <- hGetContents hOut
> case exitCode of
> ExitSuccess -> putStr $ "Success!\n" ++ exec_output
> _ -> putStrLn "Command failed"
>
>
> Derek.
>
> On Thursday, 11 December 2014 13:29:34 UTC, Ernesto Rodriguez wrote:
>
>> Hi Michael,
>>
>> Thank you for your answer. Conduits certainly are the best approach, but
>> the output I am going to read is very short and the external program will
>> only be called once or twice per execution so I think Lazy IO won't be
>> problematic here.
>>
>> Best regards,
>>
>> Ernesto
>>
> On Thu, Dec 11, 2014 at 10:18 AM, Michael Snoyman <mic... at snoyman.com>
>> wrote:
>>
> You can use readProcess[1], but it has some downsides:
>>>
>>> 1. Can't specify all options to CreateProcess, e.g., working directory
>>> and environment variables.
>>> 2. Relies on lazy I/O
>>>
>>> I use Data.Conduit.Process[2] for this kind of thing, which has a full
>>> tutorial[3]. However, it *does* involve non-base dependencies.
>>>
>>> [1] http://www.stackage.org/haddock/2014-12-10-ghc78-exc/
>>> process-1.2.0.0/System-Process.html#v:readProcess
>>> [2] http://www.stackage.org/haddock/2014-12-10-ghc78-exc/
>>> conduit-extra-1.1.4.2/Data-Conduit-Process.html
>>> [3] https://www.fpcomplete.com/user/snoyberg/library-
>>> documentation/data-conduit-process?show=tutorials
>>>
>>> On Thu Dec 11 2014 at 11:11:12 AM Ernesto Rodriguez <ne... at netowork.me>
>>> wrote:
>>>
>> Dear All,
>>>>
>>>> I have a program which invokes an external program via exec. I wish to
>>>> capture the standard output of the program without using external
>>>> (temporary) files. Is it possible to create an in memory handle which I can
>>>> tell exec to use as standard output of the invoked program. It would be
>>>> great if it can be done without any extra dependencies.
>>>>
>>>> Best,
>>>>
>>>> --
>>>> Ernesto Rodriguez
>>>>
>>>> Masters Student
>>>> Computer Science
>>>> Utrecht University
>>>>
>>>> www.netowork.me
>>>> github.com/netogallo
>>>>
>>>>
>>>> _______________________________________________
>>>> Haskell-Cafe mailing list
>>>>
>>> Haskel... at haskell.org
>>>> http://www.haskell.org/mailman/listinfo/haskell-cafe
>>>>
>>>
>>
>>
>> --
>> Ernesto Rodriguez
>>
>> Masters Student
>> Computer Science
>> Utrecht University
>>
>> www.netowork.me
>> github.com/netogallo
>>
>>
>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20141211/b74700c0/attachment.html>
More information about the Haskell-Cafe
mailing list