Can Haskell's Control.Concurrent.Async.mapConcurrently have a limit?

后端 未结 5 527
有刺的猬
有刺的猬 2021-02-04 08:45

I\'m attempting to run multiple downloads in parallel in Haskell, which I would normally just use the Control.Concurrent.Async.mapConcurrently function for. However, doing so o

5条回答
  •  南笙
    南笙 (楼主)
    2021-02-04 09:01

    This is really easy to do using the Control.Concurrent.Spawn library:

    import Control.Concurrent.Spawn
    
    type URL      = String
    type Response = String    
    
    numMaxConcurrentThreads = 4
    
    getURLs :: [URL] -> IO [Response]
    getURLs urlList = do
       wrap <- pool numMaxConcurrentThreads
       parMapIO (wrap . fetchURL) urlList
    
    fetchURL :: URL -> IO Response
    

提交回复
热议问题