O'Reilly logo

Programming Elixir 1.2 by Dave Thomas

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

When Processes Die

Who gets told when a process dies? By default, no one. Obviously the VM knows and can report it to the console, but your code will be oblivious unless you explicitly tell Elixir you want to get involved.

Here’s the default case: we spawn a function that uses the Erlang timer library to sleep for 500 ms. It then exits with a status of 99.

The code that spawns it sits in a receive. If it receives a message, it reports that fact; otherwise, after one second it lets us know that nothing happened.

 defmodule​ Link1 ​do
 import​ ​:timer​, ​only:​ [ ​sleep:​ 1 ]
 
 def​ sad_function ​do
  sleep 500
 exit​(​:boom​)
 end
 def​ run ​do
  spawn(Link1, ​:sad_function​, [])
 receive​ ​do
  msg ->
  IO.puts ...

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