forked from simonmar/parconc-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeturls.hs
45 lines (36 loc) · 1.15 KB
/
geturls.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
-- (c) Simon Marlow 2011, see the file LICENSE for copying terms.
--
-- Sample geturls.hs (CEFP summer school notes, 2011)
--
-- Downloading multiple URLs concurrently, timing the downloads
--
-- Compile with:
-- ghc -threaded --make geturls.hs
import GetURL
import TimeIt
import Control.Monad
import Control.Concurrent
import Control.Exception
import Text.Printf
import qualified Data.ByteString as B
-----------------------------------------------------------------------------
-- Our Async API:
data Async a = Async (MVar a)
async :: IO a -> IO (Async a)
async action = do
var <- newEmptyMVar
forkIO (action >>= putMVar var)
return (Async var)
wait :: Async a -> IO a
wait (Async var) = readMVar var
-----------------------------------------------------------------------------
sites = ["http://www.google.com",
"http://www.bing.com",
"http://www.yahoo.com",
"http://www.wikipedia.com/wiki/Spade",
"http://www.wikipedia.com/wiki/Shovel"]
main = mapM (async.http) sites >>= mapM wait
where
http url = do
(page, time) <- timeit $ getURL url
printf "downloaded: %s (%d bytes, %.2fs)\n" url (B.length page) time