git.iko.soy/distributed-process
on master help sign in
Fix memory leaks in the master-slave example
commit6f40a8f636b6a99a097b371d9a2a49cf0e7587f1 authorEdsko de Vries <edsko@edsko.net> dateMon 1 Oct 2012, 11:14 (14 years ago) parentddf847e
1 file changed+16−6
distributed-process-demos/src/MasterSlave/MasterSlave.hs+16−6
distributed-process-demos/src/MasterSlave/MasterSlave.hs+16−6
@@ -10,16 +10,26 @@ slave (pid, n) = send pid (numPrimeFactors n)
1010
1111remotable ['slave]
1212
13+-- | Wait for n integers and sum them all up
14+sumIntegers :: Int -> Process Integer
15+sumIntegers = go 0
16+ where
17+ go :: Integer -> Int -> Process Integer
18+ go !acc 0 = return acc
19+ go !acc n = do
20+ m <- expect
21+ go (acc + m) (n - 1)
22+
1323master :: Integer -> [NodeId] -> Process Integer
1424master n slaves = do
1525 us <- getSelfPid
1626
1727 -- Distribute 1 .. n amongst the slave processes
18− spawnLocal $ forM_ (zip [1 .. n] (cycle slaves)) $ \(m, them) ->
19− spawn them ($(mkClosure 'slave) (us, m))
28+ spawnLocal $
29+ forM_ (zip [1 .. n] (cycle slaves)) $ \(m, there) -> do
30+ spawnAsync there ($(mkClosure 'slave) (us, m))
31+ _ <- expectTimeout 0 :: Process (Maybe DidSpawn)
32+ return ()
2033
2134 -- Wait for the result
22− partials <- replicateM (fromIntegral n) (expect :: Process Integer)
23−
24− -- And return the sum
25− return (sum partials)
35+ sumIntegers (fromIntegral n)