Download Concurrency Oriented Programming in Lua

Transcript
Figure 5.4: Two process on different nodes exchange messages
running on different nodes, and each of these nodes were in different network
hosts, the previous example could be broken in two separate programs, one
for each distributed process (Figure 5.4).
The code for the pong process:
require ’concurrent’
function pong()
while true do
local msg = concurrent.receive()
if msg.body == ’finished’ then
break
elseif msg.body == ’ping’ then
print(’pong received ping’)
concurrent.send(msg.from, { body = ’pong’ })
end
end
print(’pong finished’)
end
concurrent.init(’pong@gaia’)
pid = concurrent.spawn(pong)
concurrent.register(’pong’, pid)
concurrent.loop()
concurrent.shutdown()
And the code for the ping process:
require ’concurrent’
function ping(n)
33