[Haskell-cafe] System.Process.readProcess "yes" [] "" >>= return . take 100

27 views
Skip to first unread message

David Fox

unread,
Sep 1, 2014, 9:07:55 PM9/1/14
to Haskell Cafe
I would expect the expression above to finish - but it doesn't.  Is this a bug?

Lukas Braun

unread,
Sep 1, 2014, 9:14:32 PM9/1/14
to haskel...@haskell.org
No, that's not a bug. From the documentation on «readProcess»:

> readProcess forks an external process, reads its standard output
> strictly, blocking until the process terminates, and returns the
> output string.

Since «yes» never terminates, the call to «readProcess» will block
forever.


Lukas

Thomas Hallgren

unread,
Sep 2, 2014, 6:17:04 AM9/2/14
to haskel...@haskell.org
Hi,

On 2014-09-02 03:07, David Fox wrote:
> I would expect the expression above to finish - but it doesn't. Is this a bug?
>

Below is a lazier variant of readProcess. If you use this, your example will
work as expected. You can also pipe several processes together, e.g.

readProcess (shell "yes") "" >>= readProcess (shell "head")

-- Thomas H


-- | Lazy variant of readProcess
module Process(readProcess,shell,proc) where
import System.Process hiding (readProcess)
import System.IO(hGetContents,hClose,hPutStr)
import Control.Concurrent(forkIO)
import System.IO.Error(tryIOError)

-- | Feed some input to a shell process and read the output lazily
readProcess :: CreateProcess -- ^ Process specified with 'shell' or 'proc'
-> String -- ^ input to the process
-> IO String -- ^ output from the process
readProcess proc input =
do (Just stdin,Just stdout,Nothing,ph) <-
createProcess proc{std_in=CreatePipe,std_out=CreatePipe}
forkIO $ do tryIOError $ hPutStr stdin input
tryIOError $ hClose stdin
waitForProcess ph
return ()
hGetContents stdout


_______________________________________________
Haskell-Cafe mailing list
Haskel...@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply all
Reply to author
Forward
0 new messages