Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use <code>pipes</code> to concurrently merge two input streams. The first trick is to read from two streams concurrently, which you can do using the <code>stm</code> package:</p> <pre><code>import Control.Applicative import Control.Proxy import Control.Concurrent import Control.Concurrent.STM import System.Process toTMVarC :: (Proxy p) =&gt; TMVar a -&gt; () -&gt; Consumer p a IO r toTMVarC tmvar () = runIdentityP $ forever $ do a &lt;- request () lift $ atomically $ putTMVar tmvar a fromTMVarS :: (Proxy p) =&gt; TMVar a -&gt; () -&gt; Producer p a IO r fromTMVarS tmvar () = runIdentityP $ forever $ do a &lt;- lift $ atomically $ takeTMVar tmvar respond a </code></pre> <p>I will soon provide the above primitives in a <code>pipes-stm</code> package, but use the above for now.</p> <p>Then you just feed each <code>Handle</code> to a separate <code>MVar</code> and read from both concurrently:</p> <pre><code>main = do (_, mStdout, mStderr, _) &lt;- createProcess (proc "ls" []) case (,) &lt;$&gt; mStdout &lt;*&gt; mStderr of Nothing -&gt; return () Just (stdout, stderr) -&gt; do out &lt;- newEmptyTMVarIO err &lt;- newEmptyTMVarIO forkIO $ runProxy $ hGetLineS stdout &gt;-&gt; toTMVarC out forkIO $ runProxy $ hGetLineS stderr &gt;-&gt; toTMVarC err let combine () = runIdentityP $ forever $ do str &lt;- lift $ atomically $ takeTMVar out `orElse` takeTMVar err respond str runProxy $ combine &gt;-&gt; putStrLnD </code></pre> <p>Just change out <code>putStrLnD</code> with however you want to process the input.</p> <p>To learn more about the <a href="http://hackage.haskell.org/package/pipes"><code>pipes</code> package</a>, just read <a href="http://hackage.haskell.org/packages/archive/pipes/3.0.0/doc/html/Control-Proxy-Tutorial.html">Control.Proxy.Tutorial</a>.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload