git.iko.soy/distributed-process
on master help sign in
Make all three spawning strategies available
commite7543a3e38a73a98e7500f0b69df726b9b84211e authorEdsko de Vries <edsko@edsko.net> dateMon 1 Oct 2012, 16:54 (14 years ago) parent845781c
2 files changed+26−9
distributed-process-demos/src/MasterSlave/MasterSlave.hs+21−7 distributed-process-demos/src/MasterSlave/SimpleLocalnet.hs+5−2
distributed-process-demos/src/MasterSlave/MasterSlave.hs+21−7
@@ -20,16 +20,30 @@ sumIntegers = go 0
2020 m <- expect
2121 go (acc + m) (n - 1)
2222
23−master :: Integer -> [NodeId] -> Process Integer
24−master n slaves = do
23+data SpawnStrategy = SpawnSyncWithReconnect
24+ | SpawnSyncNoReconnect
25+ | SpawnAsync
26+ deriving (Show, Read)
27+
28+master :: Integer -> SpawnStrategy -> [NodeId] -> Process Integer
29+master n spawnStrategy slaves = do
2530 us <- getSelfPid
2631
2732 -- Distribute 1 .. n amongst the slave processes
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 ()
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 ->
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
44+ spawnAsync there ($(mkClosure 'slave) (us, m))
45+ _ <- expectTimeout 0 :: Process (Maybe DidSpawn)
46+ return ()
3347
3448 -- Wait for the result
3549 sumIntegers (fromIntegral n)
distributed-process-demos/src/MasterSlave/SimpleLocalnet.hs+5−2
@@ -1,4 +1,5 @@
11import System.Environment (getArgs)
2+import Control.Exception (evaluate)
23import Control.Distributed.Process
34import Control.Distributed.Process.Node (initRemoteTable)
45import Control.Distributed.Process.Backend.SimpleLocalnet
@@ -12,10 +13,12 @@ main = do
1213 args <- getArgs
1314
1415 case args of
15− ["master", host, port, n] -> do
16+ ["master", host, port, strN, strSpawnStrategy] -> do
1617 backend <- initializeBackend host port rtable
18+ n <- evaluate $ read strN
19+ spawnStrategy <- evaluate $ read strSpawnStrategy
1720 startMaster backend $ \slaves -> do
18− result <- MasterSlave.master (read n) slaves
21+ result <- MasterSlave.master n spawnStrategy slaves
1922 liftIO $ print result
2023 ["slave", host, port] -> do
2124 backend <- initializeBackend host port rtable