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

后端 未结 5 514
有刺的猬
有刺的猬 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 08:55

    A quick solution would be to use a semaphore to restrict the number of concurrent actions. It's not optimal (all threads are created at once and then wait), but works:

    import Control.Concurrent.MSem
    import Control.Concurrent.Async
    import Control.Concurrent (threadDelay)
    import qualified Data.Traversable as T
    
    mapPool :: T.Traversable t => Int -> (a -> IO b) -> t a -> IO (t b)
    mapPool max f xs = do
        sem <- new max
        mapConcurrently (with sem . f) xs
    
    -- A little test:
    main = mapPool 10 (\x -> threadDelay 1000000 >> print x) [1..100]
    

提交回复
热议问题