Long-running Processes

Learn how to deal with long-running processes.

We'll cover the following...

Define a new module

To see how linking processes and trapping exits behave, we need processes that hang around long enough for us to observe them. IEx is a long-running process that is readily available, but we’ll need to spawn others for it to interact with.

To do that, let’s define a new module at lib/islands_engine/demo_proc.ex. All we need is a single function that implements an infinite recursion. We call it the loop/0 function. In the middle, we use receive/1 to retrieve the next message from the mailbox:

Press + to interact
defmodule IslandsEngine.DemoProc do
def loop() do
receive do
message -> IO.puts "I got a message: #{message}"
end
loop()
end
end

We don’t need an after-block here. This code will run in a separate ...