Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement Haskell equivalent of invokeAll

I'm looking for equivalent functionality to ExecutorService.invokeAll in Haskell. Do I need to implement it? From documentation:

Executes the given tasks, returning a list of Futures holding their status and results when all complete.

In my use case, tasks spend most of their time waiting on IO, so I only need to avoid constantly blocking main thread which would build up a collection of Either results or errors.

like image 810
Rumca Avatar asked Apr 10 '14 16:04

Rumca


2 Answers

The standard solution to that category of problems is the "async" library. As much as I understand your problem you need something like this:

import Control.Concurrent.Async

processActions :: [IO a] -> IO ()
processActions actions = do
  -- Execute all the actions concurrently and
  -- get a list of asyncs (an equivalent to futures):
  asyncs <- mapM async actions
  -- Do whatever you want...
  -- ...
  -- Block until all the asyncs complete:
  mapM_ wait asyncs

However the library provides a lot of other patterns, so you should just check it out. There's a possibility that what you need is the mapConcurrently function.

like image 114
Nikita Volkov Avatar answered Sep 27 '22 17:09

Nikita Volkov


No problems. Just use the async library and run mapM async someListOfJobs to get a list of Asyncs on which you can wait, poll, and many other operations.

like image 37
Thomas M. DuBuisson Avatar answered Sep 27 '22 17:09

Thomas M. DuBuisson