What's a process in Elixir?

Adolfo Neto - Mar 4 '23 - - Dev Community

To learn what a process is in Elixir, you must first know what a function is.

For example, 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
Enter fullscreen mode Exit fullscreen mode

If all goes well, it returns the :ok atom .
One function that lets you spawn a process is Kernel.spawn/1.

Again, the name is spawn. It's part of the Kernel module and takes one argument.

Can I do

Kernel.spawn(IO.puts("Adolfo"))
Enter fullscreen mode Exit fullscreen mode

?
No!

Let's start simple: every function that is part of the Kernel module doesn't need the module name before it.
You just call

elixir
spawn(IO.puts("Adolfo"))
`

It will still be wrong, but with fewer letters.

What Kernel.spawn/1` gets is an arity 0 function, i.e., one that takes no arguments.
How do we do that?
Like this:

fn -> 1 end
Enter fullscreen mode Exit fullscreen mode

The above function has no name (anonymous) and returns 1.

But see in the image that if you spawn it, nothing interesting happens:

Image description

First, see that

fn -> 1 end
Enter fullscreen mode Exit fullscreen mode

returned some sort of "code" that identifies the function:

#Function<43.3316493/0 in :erl_eval.expr/6>
Enter fullscreen mode Exit fullscreen mode

and

spawn(fn -> 1 end)
Enter fullscreen mode Exit fullscreen mode

returned a PID, a Process IDentifier:

#PID<0.120.0>
Enter fullscreen mode Exit fullscreen mode

I can assign this PID to a variable:

iex(1)> pid = spawn(fn -> 1 end)
#PID<0.110.0>
Enter fullscreen mode Exit fullscreen mode

And then ask if the process that was spawned is alive:

iex(2)> Process.alive?(pid)
false
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

But if I ask again after 10 seconds, the answer is no.

iex(5)> Process.alive?(pid)
false
Enter fullscreen mode Exit fullscreen mode

If I do this here

iex(6)> pid = spawn(fn -> Process.sleep(10000); IO.puts("Adolfo") end)
#PID<0.117.0>
Adolfo
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

Anyway, this is just the basics of the basics. Read more at
https://elixirschool.com/pt/lessons/intermediate/concurrency

Or in the Getting Started of the Elixir language
https://elixir-lang.org/getting-started/processes.html

I did all this without even mentioning send and receive.

Finally: a process in Elixir is a processing unit that performs a function.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player