Skip to content

Elixir Concurrency — spawn, send, receive, and Tasks

DodaTech Updated 2026-06-29 1 min read

In this tutorial, you will learn about Elixir Concurrency. We cover key concepts, practical examples, and best practices to help you master this topic.

Elixir's concurrency model is based on the Actor Model. Each Process is lightweight, isolated, and communicates via message passing. This makes concurrent code safe, scalable, and easy to reason about.

Elixir processes are not OS threads — they're lightweight BEAM processes that share a single OS thread.

spawn

# Spawn a process
pid = spawn(fn ->
  # This runs in a new process
  IO.puts("Hello from process #{inspect(self())}")
end)

# Send message
send(pid, {:hello, "world"})

# Receive message
receive do
  {:hello, msg} -> IO.puts("Received: #{msg}")
  after 1000 -> IO.puts("Timeout")
end

Tasks

# Fire-and-forget
Task.start(fn ->
  expensive_computation()
end)

# Await result
task = Task.async(fn ->
  heavy_work()
end)

result = Task.await(task, timeout: 5000)

# Parallel map
tasks = [1, 2, 3, 4]
|> Enum.map(&Task.async(fn -> process_item(&1) end))
|> Enum.map(&Task.await/1)

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro