DEV Community

Adolfo Neto
Adolfo Neto

Posted on

A livebook for the basics of processes in Elixir

The basics of processes in Elixir

Examples

All you have to do is to open a Livebook server and import this notebook, by clicking on the button below.

Run in Livebook

spawn(fn -> 3 end)
Enter fullscreen mode Exit fullscreen mode
pid = spawn(fn -> 3 end)
Enter fullscreen mode Exit fullscreen mode
Process.alive?(pid)
Enter fullscreen mode Exit fullscreen mode
pid = spawn(fn -> IO.puts("Adolfo") end)
Enter fullscreen mode Exit fullscreen mode
Process.alive?(pid)
Enter fullscreen mode Exit fullscreen mode
Process.sleep(10000)
Enter fullscreen mode Exit fullscreen mode
pid =
  spawn(fn ->
    IO.puts("Starting")
    Process.sleep(10000)
    IO.puts("The end")
  end)
Enter fullscreen mode Exit fullscreen mode
# click evaluate while the process is sleeping
Process.alive?(pid)
Enter fullscreen mode Exit fullscreen mode
defmodule Example do
  def listen do
    receive do
      {:ok, "hello"} -> IO.puts("World")
      _ -> listen()
    end
  end
end
Enter fullscreen mode Exit fullscreen mode
pid = spawn(Example, :listen, [])
Enter fullscreen mode Exit fullscreen mode
Process.alive?(pid)
Enter fullscreen mode Exit fullscreen mode
send(pid, :hi)
Enter fullscreen mode Exit fullscreen mode
send(pid, {:ok, "hello"})
Enter fullscreen mode Exit fullscreen mode

Top comments (0)