July 2017
Intermediate to advanced
284 pages
6h 45m
English
Remember the example where our child process didn’t loop and we sent it two messages? The first was processed, but the child exited, and we never got a response to the second message.
We can handle this by linking our original process and the child process. If we do this, and one of the processes dies, the other will be sent a message. Let’s go back to that example, and change our spawn call with spawn_link.
| | cheery = fn -> |
| | receive do |
| | { from_pid, :en, msg } -> |
| | send from_pid, "Well, hello #{msg}" |
| | { from_pid, :fr, msg } -> |
| | send from_pid, "Bonjour #{msg}" |
| | { from_pid, :es, msg } -> |
| | send from_pid, "Hola #{msg}" |
| | end |
| | end |
| | |
| | Process.flag(:trap_exit ... |
Read now
Unlock full access