O'Reilly logo

Functional Programming: A PragPub Anthology by Michael Swaine

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required