@@ -69,15 +69,15 @@ And now we have a running node.
69
69
70
70
### Sending messages
71
71
72
- We start a new process by evaluating ` forkProcess ` , which takes a node,
73
- a ` Process ` action - because our concurrent code will run in the ` Process `
74
- monad - and returns an address for the process in the form of a ` ProcessId ` .
75
- The process id can be used to send messages to the running process - here we
76
- will send one to ourselves!
72
+ We start a new process by evaluating ` runProcess ` , which takes a node and
73
+ a ` Process ` action to run, because our concurrent code will run in the
74
+ ` Process ` monad. Each process has an identifier associated to it. The process
75
+ id can be used to send messages to the running process - here we will send one
76
+ to ourselves!
77
77
78
78
{% highlight haskell %}
79
79
-- in main
80
- _ <- forkProcess node $ do
80
+ _ <- runProcess node $ do
81
81
-- get our own process id
82
82
self <- getSelfPid
83
83
send self "hello"
@@ -86,12 +86,11 @@ will send one to ourselves!
86
86
return ()
87
87
{% endhighlight %}
88
88
89
- Lightweight processes are implemented as ` forkIO ` threads. In general we will
90
- try to forget about this implementation detail, but let's note that we
91
- haven't deadlocked our own thread by sending to and receiving from its mailbox
92
- in this fashion. Sending messages is a completely asynchronous operation - even
93
- if the recipient doesn't exist, no error will be raised and evaluating ` send `
94
- will not block the caller, even if the caller is sending messages to itself!
89
+ Note that we haven't deadlocked our own thread by sending to and receiving
90
+ from its mailbox in this fashion. Sending messages is a completely
91
+ asynchronous operation - even if the recipient doesn't exist, no error will be
92
+ raised and evaluating ` send ` will not block the caller, even if the caller is
93
+ sending messages to itself!
95
94
96
95
Receiving works the opposite way, blocking the caller until a message
97
96
matching the expected type arrives in our (conceptual) mailbox. If multiple
@@ -118,7 +117,7 @@ main :: IO ()
118
117
main = do
119
118
Right t <- createTransport "127.0.0.1" "10501" defaultTCPParameters
120
119
node <- newLocalNode t initRemoteTable
121
- forkProcess node $ do
120
+ runProcess node $ do
122
121
-- Spawn another worker on the local node
123
122
echoPid <- spawnLocal $ forever $ do
124
123
-- Test our matches in order against each message in the queue
@@ -138,13 +137,7 @@ main = do
138
137
case m of
139
138
-- Die immediately - throws a ProcessExitException with the given reason.
140
139
Nothing -> die "nothing came back!"
141
- (Just s) -> say $ "got " ++ s ++ " back!"
142
- return ()
143
-
144
- -- A 1 second wait. Otherwise the main thread can terminate before
145
- -- our messages reach the logging process or get flushed to stdio
146
- liftIO $ threadDelay (1* 1000000)
147
- return ()
140
+ Just s -> say $ "got " ++ s ++ " back!"
148
141
{% endhighlight %}
149
142
150
143
Note that we've used the ` receive ` class of functions this time around.
0 commit comments