[Haskell-cafe] Questions about concurrency and laziness

Dan Frumin notdan at covariant.me
Thu Jun 11 15:55:45 UTC 2015


On 06/11, Tony Garcia wrote:
> I see, so I might put a thunk in the MVar but the IO action that I wanted
> to do in parallel would be executed when I get to the putMVar anyway.
> 

Exactly. That's the whole trick with the monads. You can't 
execute IO code just by reducing/evaluating the expression.

> OK, thank you!
> Jose.
> 
> On Thu, 11 Jun 2015 at 12:44 Roman Cheplyaka <roma at ro-che.info> wrote:
> 
> > You may be confusing execution of IO actions with evaluation.
> >
> > Looking at the source of getURL, its result probably *will* be a thunk:
> > one that computes "rspBody rsp" when evaluated.
> >
> > But why would that bother you?
> >
> > The IO effects of getURL, though, are a completely different beast.
> > Unless you practice some dirty magic or use lazy IO, sequenced IO
> > actions are executed in order. So, by the time execution reaches
> > putMVar, the effects of getURL (ie, downloading) will have finished.
> >
> >
> > On 11/06/15 14:35, Tony Garcia wrote:
> > > Hi,
> > >
> > > First of all, apologies if this is not the right place to ask this
> > question.
> > >
> > > I'm reading the book "Parallel and concurrent programming in Haskell"
> > > and I'm having some issues reasoning about the impact of laziness.
> > >
> > > Looking at the first example in chapter 8 [1]
> > >
> > > main = do
> > >   m1 <- newEmptyMVar
> > >   m2 <- newEmptyMVar
> > >
> > >   forkIO $ do
> > >     r <- getURL "http://www.wikipedia.org/wiki/Shovel"
> > >     putMVar m1 r
> > >
> > >   forkIO $ do
> > >     r <- getURL "http://www.wikipedia.org/wiki/Spade"
> > >     putMVar m2 r
> > >
> > >   r1 <- takeMVar m1
> > >   r2 <- takeMVar m2
> > >   print (B.length r1, B.length r2)
> > >
> > > I don't understand why this function is not just putting an unevaluated
> > > thunk in the MVar. Well, I assume that getURL is an eager function, but
> > > looking at its code [2] or at the documentation of Network.Browser [3] I
> > > don't see why...
> > >
> > > Am I looking at this wrong? Is there any rule of thumb to be used in
> > > these cases? I have the impression that it's really easy to end up
> > > creating thunks in parallel threads and evaluating them in the main
> > one...
> > >
> > > Thanks,
> > > Jose.
> > >
> > > [1] http://chimera.labs.oreilly.com/books/1230000000929/ch08.html
> > > [2] https://github.com/simonmar/parconc-examples/blob/master/GetURL.hs
> > > [3]
> > >
> > http://hackage.haskell.org/package/HTTP-4000.0.8/docs/Network-Browser.html
> > >
> > >
> > >
> > > _______________________________________________
> > > Haskell-Cafe mailing list
> > > Haskell-Cafe at haskell.org
> > > http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> > >
> >
> >
> >

> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe



More information about the Haskell-Cafe mailing list