Long-running Processes
Explore how to implement and manage long-running processes in Elixir using recursion and receive blocks. Understand process linking, trapping exits, and supervising processes to ensure system reliability and graceful handling of process crashes.
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:
We don’t need an after-block here. This code will run in a ...