Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7435271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:02:47+00:00 2026-05-29T10:02:47+00:00

Sometimes I want to run a maximum amount of IO actions in parallel at

  • 0

Sometimes I want to run a maximum amount of IO actions in parallel at once for network-activity, etc. I whipped up a small concurrent thread function which works well with https://gist.github.com/810920, but this isn’t really a pool as all IO actions must finish before others can start.

The type of what I’m looking for would be something like:

runPool :: Int -> [IO a] -> IO [a]

and should be able to operate on finite and infinite lists.

The pipes package looks like it would be able to achieve this quite well, but I feel there is probably a similar solution to the gist I have provided just using mvars, etc, from the haskell-platform.

Has anyone encountered an idiomatic solution without any heavy dependencies?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T10:02:47+00:00Added an answer on May 29, 2026 at 10:02 am

    You need a thread pool, if you want something short, you could get inspiration from Control.ThreadPool (from the control-engine package which also provide more general functions), for instance threadPoolIO is just :

    threadPoolIO :: Int -> (a -> IO b) -> IO (Chan a, Chan b)
    threadPoolIO nr mutator = do
        input <- newChan
        output <- newChan
        forM_ [1..nr] $
            \_ -> forkIO (forever $ do
                i <- readChan input
                o <- mutator i
                writeChan output o)
        return (input, output)
    

    It use two Chan for communication with the outside but that’s usually what you want, it really help writing code that don’t mess up.

    If you absolutely want to wrap it up in a function of your type you can encapsulate the communication too :

    runPool :: Int -> [IO a] -> IO [a]
    runPool n as = do
      (input, output) <- threadPoolIO n (id)
      forM_ as $ writeChan input
      sequence (repeat (length as) $ readChan output)
    

    This won’t keep the order of your actions, is that a problem (it’s easy to correct by transmitting the index of the action or just using an array instead to store the responses) ?

    Note : the n threads will stay alive forever with this simplistic version, adding a “killAll” returned action to threadPoolIO would resolve this problem handily if you intend to create and trash several of those pool in a long running application (if not, given the weight of threads in Haskell, it’s probably not worth the bother).
    Note that this function works on finite lists only, that’s because IO is normally strict so you can’t start to process elements of IO [a] before the whole list is produced, if you really want that you’ll have either to use lazy IO with unsafeInterleaveIO (maybe not the best idea) or completely change your model and use something like conduits to stream your results.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I sometimes want to make small edits to small file from within the shell
What I want to do : run a background thread which calculates ListView contents
Basically, do I have to put code I want to run on another thread
Sometimes I want to run bundle update but only to see which gems need
I have a process where I want the main thread to run through a
If I'm not online, I sometimes want to package some changes to a commit,
When using implicit waits, as advised here , I still sometimes want to assert
Sometimes I want to have temporary comments fully left justified on a line (//)
Sometimes I want to return the value that is a property of an object
Sometimes I want to build Python or GCC from scratch just for fun, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.