-
Notifications
You must be signed in to change notification settings - Fork 103
/
geturlscancel2.hs
88 lines (73 loc) · 2.26 KB
/
geturlscancel2.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
{-# LANGUAGE DeriveDataTypeable, CPP #-}
-- (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,
-- and the user may press 'q' to stop the downloads at any time.
--
-- Compile with:
-- ghc -threaded --make geturlscancel.hs
import GetURL
import TimeIt
import Data.Either
import System.IO
import Control.Monad
#if __GLASGOW_HASKELL__ < 706
import Control.Concurrent
#else
-- forkFinally was added in GHC 7.6
import Control.Concurrent hiding (forkFinally)
#endif
import Control.Exception
import Text.Printf
import qualified Data.ByteString as B
import Data.Typeable
import Prelude hiding (catch)
-----------------------------------------------------------------------------
-- Our Async API:
-- <<Async
data Async a = Async ThreadId (MVar (Either SomeException a))
-- >>
-- <<forkFinally
forkFinally :: IO a -> (Either SomeException a -> IO ()) -> IO ThreadId
forkFinally action fun =
mask $ \restore ->
forkIO (do r <- try (restore action); fun r)
-- >>
-- <<async
async :: IO a -> IO (Async a)
async action = do
m <- newEmptyMVar
t <- forkFinally action (putMVar m)
return (Async t m)
-- >>
-- <<waitCatch
waitCatch :: Async a -> IO (Either SomeException a)
waitCatch (Async _ var) = readMVar var
-- >>
-- <<cancel
cancel :: Async a -> IO ()
cancel (Async t var) = throwTo t ThreadKilled
-- >>
-----------------------------------------------------------------------------
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"]
timeDownload :: String -> IO ()
timeDownload url = do
(page, time) <- timeit $ getURL url
printf "downloaded: %s (%d bytes, %.2fs)\n" url (B.length page) time
-- <<main
main = do
as <- mapM (async . timeDownload) sites -- <1>
forkIO $ do -- <2>
hSetBuffering stdin NoBuffering
forever $ do
c <- getChar
when (c == 'q') $ mapM_ cancel as
rs <- mapM waitCatch as -- <3>
printf "%d/%d succeeded\n" (length (rights rs)) (length rs) -- <4>
-- >>