Reference: Parallel and Concurrent Programming In Haskell
Distributed programming is running program on multiple machines simultaneously. We want to do that because
Utilizes more power of paralellism
Distributed servers speed up client response
Distributed program can exploit heterogeneous enviroment
Core API distributed-process
(independent of transport layer)
Use together with transport layer that provides sending and receiving messages between nodes
Threads are always created on the current node, process can be created on a remote node.
A distributed program consists of a set of processes that may communicate with one another by messages.
Each process has a unique ProcessId
A ping-pong message exchange.
A single master process creates a child process
M —ping–> C
M <–pong— C
exit
data Process
data NodeId
data ProcessId
getSelfPid :: Process ProcessId
getSelfNode :: Process NodeId
spawn :: NodeId -> Closure (Process ()) -> Process ProcessId
send :: Serializable a => ProcessId -> a -> Process ()
expect :: Serializable a => Process a
terminate :: Process a
say :: String -> Process ()
data Message
= Ping ProcessId
| Pong ProcessId
deriving (Typeable, Generic)
deriving anyclass Binary
pingServer :: Process ()
= do
pingServer Ping from <- expect
$ printf "ping received from %s" (show from)
say <- getSelfPid
mypid Pong mypid) send from (