Skip to content

Instantly share code, notes, and snippets.

@Rydgel
Created August 12, 2015 08:12
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Rydgel/938f8479f433accc9ca0 to your computer and use it in GitHub Desktop.
Save Rydgel/938f8479f433accc9ca0 to your computer and use it in GitHub Desktop.
haskell async http requests example
{-# OPTIONS_GHC -Wall -O2 -threaded -with-rtsopts="-N" #-}
import Control.Concurrent
import Control.Concurrent.Async
import Control.Lens
import Control.Monad
import Data.ByteString.Lazy hiding (replicate)
import Data.List.Split
import Network.Wreq hiding (getWith)
import Network.Wreq.Session
data ThreadOptions = ThreadOptions { wreqOptions :: Options, interval :: Int }
data GlobalOptions = GlobalOptions { threadOptions :: ThreadOptions, threadCount :: Int }
defaultWreqOptions :: Options
defaultWreqOptions = defaults
defaultThreadOptions :: ThreadOptions
defaultThreadOptions = ThreadOptions { wreqOptions = defaultWreqOptions, interval = 0 }
defaultGlobalOptions :: GlobalOptions
defaultGlobalOptions = GlobalOptions { threadOptions = defaultThreadOptions, threadCount = 8 }
thread :: ThreadOptions -> [String] -> IO [ByteString]
thread options urls = withSession $ \session ->
mapM (\url -> const . flip (^.) responseBody <$>
getWith (wreqOptions options) session url
<*> threadDelay (interval options)) urls
threads :: GlobalOptions -> [String] -> IO [ByteString]
threads options urls = join <$> mapConcurrently (thread (threadOptions options)) (chunksOf (threadCount options) urls)
main :: IO ()
main = do
contents <- threads defaultGlobalOptions (replicate 8 "https://httpbin.org/")
print contents
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment