Monitoring Your Processes
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 ... |
Get Functional Programming: A PragPub Anthology now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.