git.iko.soy/distributed-process
on master help sign in
Make all three spawning strategies available
e7543a3Edsko de Vries
distributed-process-demos/src/MasterSlave/MasterSlave.hs+21−7
@@ -20,16 +20,30 @@ sumIntegers = go 0
20 m <- expect
20 m <- expect
21 go (acc + m) (n - 1)
21 go (acc + m) (n - 1)
22
22
23+data SpawnStrategy = SpawnSyncWithReconnect
24+ | SpawnSyncNoReconnect
25+ | SpawnAsync
26+ deriving (Show, Read)
27+
23−master :: Integer -> [NodeId] -> Process Integer
28+master :: Integer -> SpawnStrategy -> [NodeId] -> Process Integer
24−master n slaves = do
29+master n spawnStrategy slaves = do
25 us <- getSelfPid
30 us <- getSelfPid
26
31
27 -- Distribute 1 .. n amongst the slave processes
32 -- Distribute 1 .. n amongst the slave processes
28− spawnLocal $
33+ spawnLocal $ case spawnStrategy of
34+ SpawnSyncWithReconnect ->
35+ forM_ (zip [1 .. n] (cycle slaves)) $ \(m, there) -> do
36+ them <- spawn there ($(mkClosure 'slave) (us, m))
37+ reconnect them
38+ SpawnSyncNoReconnect ->
29− forM_ (zip [1 .. n] (cycle slaves)) $ \(m, there) -> do
39+ forM_ (zip [1 .. n] (cycle slaves)) $ \(m, there) -> do
40+ _them <- spawn there ($(mkClosure 'slave) (us, m))
41+ return ()
42+ SpawnAsync ->
43+ forM_ (zip [1 .. n] (cycle slaves)) $ \(m, there) -> do
30− spawnAsync there ($(mkClosure 'slave) (us, m))
44+ spawnAsync there ($(mkClosure 'slave) (us, m))
31− _ <- expectTimeout 0 :: Process (Maybe DidSpawn)
45+ _ <- expectTimeout 0 :: Process (Maybe DidSpawn)
32− return ()
46+ return ()
33
47
34 -- Wait for the result
48 -- Wait for the result
35 sumIntegers (fromIntegral n)
49 sumIntegers (fromIntegral n)
distributed-process-demos/src/MasterSlave/SimpleLocalnet.hs+5−2
@@ -1,4 +1,5 @@
1import System.Environment (getArgs)
1import System.Environment (getArgs)
2+import Control.Exception (evaluate)
2import Control.Distributed.Process
3import Control.Distributed.Process
3import Control.Distributed.Process.Node (initRemoteTable)
4import Control.Distributed.Process.Node (initRemoteTable)
4import Control.Distributed.Process.Backend.SimpleLocalnet
5import Control.Distributed.Process.Backend.SimpleLocalnet
@@ -12,10 +13,12 @@ main = do
12 args <- getArgs
13 args <- getArgs
13
14
14 case args of
15 case args of
15− ["master", host, port, n] -> do
16+ ["master", host, port, strN, strSpawnStrategy] -> do
16 backend <- initializeBackend host port rtable
17 backend <- initializeBackend host port rtable
18+ n <- evaluate $ read strN
19+ spawnStrategy <- evaluate $ read strSpawnStrategy
17 startMaster backend $ \slaves -> do
20 startMaster backend $ \slaves -> do
18− result <- MasterSlave.master (read n) slaves
21+ result <- MasterSlave.master n spawnStrategy slaves
19 liftIO $ print result
22 liftIO $ print result
20 ["slave", host, port] -> do
23 ["slave", host, port] -> do
21 backend <- initializeBackend host port rtable
24 backend <- initializeBackend host port rtable