<div dir="auto">That makes sense. Thanks a lot, Patrick!</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Mar 19, 2020, 11:01 PM Patrick Chilton <<a href="mailto:chpatrick@gmail.com">chpatrick@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div dir="ltr"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Is there a way to get the result of "either of three calls to getUrl" as opposed to "all three calls"?<br>'Cause that's what Mike seems to want.</blockquote><div><br></div><div>That's what the Alternative instance of Concurrently is for. The result of `doA <|> doB` is the result of the argument that finished first. (<|>) = race.</div><div><br></div><div>firstResult <- runConcurrently (Concurrently (getURL "url1") <|> Concurrently (getURL "url2") <|> Concurrently (getURL "url3"))</div><div><br></div><div>or</div><div><br></div><div>firstResult <- runConcurrently $ asum $ map (Concurrently . getURL) [ "url1", "url2", "url3" ] </div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, Mar 20, 2020 at 9:44 AM Artem Pelenitsyn <<a href="mailto:a.pelenitsyn@gmail.com" target="_blank" rel="noreferrer">a.pelenitsyn@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div>Hey Niklas,</div><div><br></div><div>Thanks for your explanation -- it was useful to read.<br></div><div><br></div><div>Is there a way to get the result of "either of three calls to getUrl" as opposed to "all three calls"?</div><div>'Cause that's what Mike seems to want.</div><div><br></div><div>--</div><div>Best regards, <br></div><div>Artem<br></div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, 19 Mar 2020 at 21:38, Niklas Hambüchen <<a href="mailto:mail@nh2.me" target="_blank" rel="noreferrer">mail@nh2.me</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hey Mike,<br>
<br>
> f <- async getPages<br>
> cancel f<br>
<br>
you haven't read the PR I linked carefully enough yet :)<br>
<br>
It essentially says to never use the `async` function, as that is almost never what you want<br>
<br>
Full docs, with what you need to read highlighted:<br>
<a href="https://github.com/nh2/async/blob/1d96dc555b70a4c5aba07f15d0c7895545eb8582/Control/Concurrent/Async.hs#L21-L143" rel="noreferrer noreferrer" target="_blank">https://github.com/nh2/async/blob/1d96dc555b70a4c5aba07f15d0c7895545eb8582/Control/Concurrent/Async.hs#L21-L143</a><br>
<br>
> Am I guaranteed the the getURL() calls will definitely have either finished, or cancel?<br>
<br>
For<br>
<br>
do<br>
(page1, page2, page3) <- runConcurrently $<br>
(,,)<br>
<$> Concurrently (getURL "url1")<br>
<*> Concurrently (getURL "url2")<br>
<*> Concurrently (getURL "url3")<br>
<br>
you are guaranteed that when `runConcurrently` returns, all 3 downloads have finished successfully.<br>
<br>
Only the `instance Alternative Concurrently` (that is, when you use <|>) is implemented in terms of `race`.<br>
<br>
You are using <*>, as implemented in the `instance Applicative Concurrently`, which uses `concurrently`, which waits for both of two actions.<br>
<br>
Your code in<br>
<br>
> main = do <br>
> f <- async getPages<br>
> cancel f<br>
<br>
does not make much sense:<br>
Here you're starting a thread that would go off do something, but then you immediately cancel that thread, so it won't achieve anything.<br>
<br>
You do not need to wrap things into additional `async`s. You can directly do, for example:<br>
<br>
main :: IO ()<br>
main = do<br>
(page1, page2, page3) <- runConcurrently $<br>
(,,)<br>
<$> Concurrently (getURL "url1")<br>
<*> Concurrently (getURL "url2")<br>
<*> Concurrently (getURL "url3")<br>
putStrLn "Here are the page contents:"<br>
putStrLn page1<br>
putStrLn page2<br>
putStrLn page3<br>
<br>
Hope that helps!<br>
<br>
Niklas<br>
_______________________________________________<br>
Haskell-Cafe mailing list<br>
To (un)subscribe, modify options or view archives go to:<br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe" rel="noreferrer noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe</a><br>
Only members subscribed via the mailman list are allowed to post.</blockquote></div>
_______________________________________________<br>
Haskell-Cafe mailing list<br>
To (un)subscribe, modify options or view archives go to:<br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe" rel="noreferrer noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe</a><br>
Only members subscribed via the mailman list are allowed to post.</blockquote></div></div>
</blockquote></div>