First we make the very simplest example of Elixir processes possible, and then we build on it. Our module will include three spawnable processes—a greeter and a couple of counters.
Source code for this episode
The greeter
Our very first process is called greeter
. It will respond to any message sent to it by outputting "Hello, friend!" and then listening for the next message.
The counters
Then we'll make slightly more complex processes. They'll receive a single value, output it and then recursively call themselves with the next value and listen for the subsequent message.
This episode covers how to send, receive, spawn and kill Elixir processes by using passing a pid
(process ID) into standard Elixir library functions.
Challenge
Create a new module that spawns counter processes like the one in this lesson but don't spawn it manually. Instead make a "controller" process that spawns a counter when it receives the message :start
and kills the counter when it receives the message :stop
.
(solution here)
No Comments