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