Elixir Processes and Message Passing
Learn about lightweight Elixir processes, how they communicate, and the principles of 'share nothing' concurrency.
Meet Elixir Processes
Elixir is built for concurrency, and processes are its core building blocks. Think of them as tiny, isolated programs running simultaneously.
Unlike operating system processes or threads, Elixir processes are incredibly lightweight. You can have hundreds of thousands, even millions, running on a single machine!
- Isolation: Each process has its own memory.
- Communication: They talk to each other by sending messages.
- Fault Tolerance: If one crashes, it doesn't bring down others.
Creating New Processes
We create a new Elixir process using the spawn function. It takes a function (often an anonymous function) that the new process will execute.
spawn returns a Process Identifier (PID), which is like an address for the new process.
defmodule MyModule do
def greet do
IO.puts "Hello from a new process!"
end
def run do
# Spawn a new process that calls MyModule.greet()
pid = spawn(MyModule, :greet, [])
IO.puts "Spawned process with PID: #{inspect(pid)}"
end
end
MyModule.run()All lessons in this course
- Elixir Processes and Message Passing
- Implementing GenServer Behavior
- Supervisors and Application Structure
- Concurrent Work with Task and Agent