[Haskell-beginners] merge two handles

David McBride dmcbride at neondsl.com
Mon Jun 13 22:59:11 CEST 2011


The IO Channel antoine recommended is a very elegant solution to this.

main = do
  let cmd = ..., parms =...
  (_ ,Just hout ,Just herr ,p) <- createProcess (proc cmd parms)
  chan <- newTChanIO :: IO (TChan String)
  makeThread hout chan
  makeThread herr chan
  forever $ atomically $ readTchan chan >>= print

makeThread handle chan = forkIO $ forever $ do
  hGetLine handle >>= atomically (writeTchan chan)

I haven't run this code, but this is the general idea that I've used
many times.  The threads run taking input from their sources and
shoving it down the channel.  The main thread continually reads
messages from the channel and blocks while it waits for new messages
to arrive.


On Mon, Jun 13, 2011 at 2:14 PM, Manfred Lotz <manfred.lotz at arcor.de> wrote:
> On Mon, 13 Jun 2011 12:51:02 -0500
> Antoine Latter <aslatter at gmail.com> wrote:
>
>> On Mon, Jun 13, 2011 at 12:35 PM, Manfred Lotz
>> <manfred.lotz at arcor.de> wrote:
>> > Hi all,
>> > I have two handles where I get stdout resp. stderr from a command
>> > output.
>> >
>> >
>> > Is it possible to merge those two handles so that I get a new input
>> > handle in a way that data can be read from the new handle whenever
>> > it is available from either of the two original handles?
>> >
>> >
>>
>> Does what you're doing strictly need to be a handle?
>>
>> One thing you could try is is using an IO Channel:
>> http://hackage.haskell.org/packages/archive/base/latest/doc/html/Control-Concurrent-Chan.html
>>
>
> Hmm, not quite sure. I was asking about handles because I use:
>
> (_ ,Just hout ,Just herr ,p) <- createProcess (proc cmd parms) {
>    std_out = CreatePipe,
>    std_err = CreatePipe
>  }
>
>
> which gives me handles to deal with.
>
>
> --
> Manfred
>
>
>
>
>
>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>



More information about the Beginners mailing list