git.iko.soy/distributed-process
on master help sign in
Fix memory leaks in the master-slave example
6f40a8fEdsko de Vries
distributed-process-demos/src/MasterSlave/MasterSlave.hs+16−6
@@ -10,16 +10,26 @@ slave (pid, n) = send pid (numPrimeFactors n)
10
10
11remotable ['slave]
11remotable ['slave]
12
12
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+
13master :: Integer -> [NodeId] -> Process Integer
23master :: Integer -> [NodeId] -> Process Integer
14master n slaves = do
24master n slaves = do
15 us <- getSelfPid
25 us <- getSelfPid
16
26
17 -- Distribute 1 .. n amongst the slave processes
27 -- Distribute 1 .. n amongst the slave processes
28+ spawnLocal $
18− spawnLocal $ forM_ (zip [1 .. n] (cycle slaves)) $ \(m, them) ->
29+ forM_ (zip [1 .. n] (cycle slaves)) $ \(m, there) -> do
19− spawn them ($(mkClosure 'slave) (us, m))
30+ spawnAsync there ($(mkClosure 'slave) (us, m))
31+ _ <- expectTimeout 0 :: Process (Maybe DidSpawn)
32+ return ()
20
33
21 -- Wait for the result
34 -- Wait for the result
22− partials <- replicateM (fromIntegral n) (expect :: Process Integer)
35+ sumIntegers (fromIntegral n)
23−
24− -- And return the sum
25− return (sum partials)