A Simple Process

Learn about working processes in Elixir.

One of Elixir’s key features is the idea of packaging code into small chunks that can be run independently and concurrently.

Concurrent programming is known to be difficult, and there’s a performance penalty to pay when we create lots of processes.

Elixir doesn’t have these issues, thanks to the architecture of the Erlang VM on which it runs. Elixir uses the actor model of concurrency. An actor is an independent process that shares nothing with any other process. We can spawn new processes, send them messages, and receive messages back. And that’s it.

In the past, we may have had to use threads or operating system processes to achieve concurrency. Each time, it might’ve felt very risky. There was so much that could go wrong. But that worry just evaporates in Elixir. In fact, Elixir developers are so comfortable creating new processes that they’ll often do it at times when we would’ve created an object in a language such as Java.

One more thing: when we talk about processes in Elixir, we aren’t talking about native operating system processes. These are too slow and bulky. Instead, Elixir uses process support in Erlang. These processes will run across all our CPUs (just like native processes), but they have very little overhead. As we’ll cover a bit later, it’s very easy to create hundreds of thousands of Elixir processes on even a modest computer, as illustrated below:

Create a simple process

Here’s a module that defines a function we’d like to run as a separate process:

defmodule SpawnBasic do 
  def greet do
    IO.puts
...