To learn what a process is in Elixir, you must first know what a function is.
IO.puts/1 is a function that writes something to the screen. The function is called
puts, it is in the
IO module (input I and output O), and it takes one argument:
iex(1)> IO.puts("Adolfo") Adolfo :ok
If all goes well, it returns the
:ok atom .
One function that lets you spawn a process is
Again, the name is
spawn. It's part of the
Kernel module and takes one argument.
Can I do
Let's start simple: every function that is part of the
Kernel module doesn't need the module name before it.
You just call
It will still be wrong, but with fewer letters.
Kernel.spawn/1` gets is an arity 0 function, i.e., one that takes no arguments.
How do we do that?
fn -> 1 end
The above function has no name (anonymous) and returns 1.
But see in the image that if you spawn it, nothing interesting happens:
First, see that
fn -> 1 end
returned some sort of "code" that identifies the function:
#Function<43.3316493/0 in :erl_eval.expr/6>
spawn(fn -> 1 end)
returned a PID, a Process IDentifier:
I can assign this PID to a variable:
iex(1)> pid = spawn(fn -> 1 end) #PID<0.110.0>
And then ask if the process that was spawned is alive:
iex(2)> Process.alive?(pid) false
It is not alive because it was a very fast function, which only returned 1.
I can, for example, make the process "sleep" for 10 seconds before returning 1.
iex(3)> pid = spawn(fn -> Process.sleep(10000); 1 end) #PID<0.113.0>
If I quickly ask if the process, whose identifier is in the pid variable, is alive, the answer is yes.
iex(4)> Process.alive?(pid) true
But if I ask again after 10 seconds, the answer is no.
iex(5)> Process.alive?(pid) false
If I do this here
iex(6)> pid = spawn(fn -> Process.sleep(10000); IO.puts("Adolfo") end) #PID<0.117.0> Adolfo
it will take 10 seconds for "Adolfo" to be written to the screen.
Whereas if I do the following, it will be immediate for "Adolfo" to appear on the screen.
iex(7)> pid = spawn(fn -> IO.puts("Adolfo") end) Adolfo #PID<0.119.0>
Anyway, this is just the basics of the basics. Read more at
Or in the Getting Started of the Elixir language
I did all this without even mentioning
Finally: a process in Elixir is a processing unit that performs a function.