[Haskell-beginners] How to wait till a process is finished before invoking the next one?

Brandon S. Allbery KF8NH allbery at ece.cmu.edu
Thu May 7 17:25:40 EDT 2009


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On May 7, 2009, at 14:53 , Thomas Friedrich wrote:
> writeFeatures :: [String] -> IO ()
> writeFeatures cs = Exc.bracket (openFile training AppendMode) hClose  
> (\h -> goo h)
> where
>   goo h = go 1 cs
>     where
>       go :: Int -> [String] -> IO ()
>       go n [] = putStrLn "Features written."
>       go n (c:cs) = do
>         features <- makeFeatures n c    -- makeFeatures :: Int ->  
> String -> IO String
>         hPutStr h features
>         go (n+1) cs
>
> And the file that is produced here is needed in the next function.

You probably want to rethink how you're doing this.  My own thought is  
that you have something like:

 > runThis :: FilePath -> [String] -> String -> IO (MVar String)
 > runThis cmd args inp = do
 >   mvar <- newEmptyMVar
 >   forkIO (readProcess cmd args inp >>= writeMVar mvar)
 >   return mvar

runOne launches in the background, you synchronize by doing takeMVar  
on the returned MVar (which will give you the output, if any).  Even  
if there is no useful output you can still create data dependencies to  
insure things wait for what they need --- and that is exactly what you  
want to do:  insure that there are data dependencies to constrain when  
programs are run.

Otherwise, you'll have to settle for linear execution.

- --
brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery at kf8nh.com
system administrator [openafs,heimdal,too many hats] allbery at ece.cmu.edu
electrical and computer engineering, carnegie mellon university    KF8NH


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.10 (Darwin)

iEYEARECAAYFAkoDUeAACgkQIn7hlCsL25VH/gCghNldOSJnChoHrJwjeaboseU4
Z28An39bR2DAAlP6K9g00eb+NnHhKZSU
=NHpz
-----END PGP SIGNATURE-----


More information about the Beginners mailing list