Starting Processes with Links
Let's learn how our supervisors use notifications to handle failures.
We'll cover the following...
We'll cover the following...
Notifications
Supervisors are built on notification, an interesting primitive. Remember, when a process uses start_link/2
to create a child process, the Erlang BEAM will notify the parent process when the child process ends. This capability can work in a couple of ways.
spawn_link
First, using spawn_link
causes all linked processes to end with the same error if any one of them ends. That may sound like a weird behavior to want, but if the top-level processes of our system end, we don’t want their child processes carrying on without them.
Here’s how spawn_link
works. Let’s spawn a process that crashes, like this:
Executable
Press + to interact
spawn fn -> raise "boom" end
Output
iex(1)> spawn fn -> raise "boom" end
#PID<0.86.0>
iex(2)>
10:45:24.797 [error] Process #PID<0.86.0> raised
...