Starting Processes with Links
Explore how to start and link processes in Elixir using spawn_link and spawn_monitor. Understand the notification mechanisms between parent and child processes and how supervisors utilize these to handle failures effectively within OTP systems.
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
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 ...