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

Thomas Hallgren hallgren at chalmers.se
Tue Sep 2 10:16:30 UTC 2014


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




More information about the Haskell-Cafe mailing list