Creating processes

You create a process by calling the Kernel.spawn/1 function, which receives an anonymous function with no arguments:

iex> self()#PID<0.85.0>iex> spawn(fn ->...>   :timer.sleep(2000)...>   IO.puts "I'm running in process #{inspect(self())}"...> end)#PID<0.91.0>I'm running in process #PID<0.91.0>

In this example, we're first calling self(), which returns the PID of the current process. In this case, it returned the PID of the shell process. Then, we use the spawn function to create a new process. As previously stated, it receives an anonymous function that will run in the newly created process. The anonymous function that we're passing to spawn uses the timer Erlang library to sleep for two seconds, and then uses the puts function ...

Get Mastering Elixir 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.