Running processes concurrently

From HaskellWiki
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

This example forks many processes in parallel, then waits for each process to exit. Finally, it prints the number of failures.

import System.Process
import Control.Concurrent
import System.Exit

main = do
        let cmds = replicate 100 "sleep 10s"
        ps <- mapM runCommand cmds
        es <- mapM waitForProcess ps
        case length . filter (/= ExitSuccess) $ es of
            0 -> putStrLn "All commands ran successfully."
            n -> putStrLn ("There were " ++ show n ++ " failures.")