Concurrent Work with Task and Agent
Run code concurrently and manage simple shared state using the Task and Agent abstractions built on top of OTP.
Concurrent Work with Task and Agent is a free Elixir & Phoenix: Scalable Backend Development lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Elixir & Phoenix: Scalable Backend Development learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Beyond Raw spawn
You can start processes with spawn, but OTP gives higher-level tools. Task runs concurrent work and collects results cleanly.
Fire-and-Forget Tasks
Task.start/1 runs a function in a new process when you do not need the result.
Task.start(fn -> IO.puts("running in background") end)Async and Await
Task.async/1 starts work and returns a struct; Task.await/1 blocks until the result is ready.
task = Task.async(fn -> 2 + 2 end)
result = Task.await(task)
IO.inspect(result)Running Tasks in Parallel
Start several tasks, then await them all. The total time is roughly the slowest task, not the sum.
tasks = Enum.map(1..3, fn n ->
Task.async(fn -> n * n end)
end)
IO.inspect(Enum.map(tasks, &Task.await/1))Task.await_many
Task.await_many/1 waits for a list of tasks and returns their results in order.
tasks = for n <- 1..3, do: Task.async(fn -> n * 10 end)
IO.inspect(Task.await_many(tasks))Timeouts
Task.await/2 takes a timeout in milliseconds (default 5000). If exceeded it exits, signalling the work took too long.
task = Task.async(fn -> :timer.sleep(50); :done end)
IO.inspect(Task.await(task, 1000))Streaming Concurrency
Task.async_stream/3 maps a function over a collection concurrently with a bounded number of workers.
1..5
|> Task.async_stream(fn n -> n * n end)
|> Enum.map(fn {:ok, v} -> v end)
|> IO.inspect()Introducing Agent
An Agent wraps mutable state behind a process, giving simple shared state without writing a full GenServer.
{:ok, pid} = Agent.start_link(fn -> 0 end)
IO.inspect(Agent.get(pid, & &1))Updating Agent State
Agent.update/2 transforms the state; Agent.get/2 reads it. The agent serializes access for safety.
{:ok, pid} = Agent.start_link(fn -> 0 end)
Agent.update(pid, &(&1 + 5))
IO.inspect(Agent.get(pid, & &1))get_and_update
Agent.get_and_update/2 reads and writes atomically, returning a value while changing state.
{:ok, pid} = Agent.start_link(fn -> 10 end)
old = Agent.get_and_update(pid, fn s -> {s, s + 1} end)
IO.inspect({old, Agent.get(pid, & &1)})Task vs Agent vs GenServer
Choose the lightest tool:
- Task for one-off concurrent work
- Agent for simple shared state
- GenServer when you need custom messages and lifecycle
Quick Check
Test your Task knowledge.
Recap
You ran concurrent code with OTP abstractions:
Task.async/awaitfor concurrent work and resultsTask.async_streamfor bounded parallel mappingAgentfor simple, safe shared state- Pick Task, Agent, or GenServer by how much control you need
Frequently asked questions
Is the “Concurrent Work with Task and Agent” lesson free?
Yes — the full text of “Concurrent Work with Task and Agent” is free to read here on the web, and the Elixir & Phoenix: Scalable Backend Development course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Elixir & Phoenix: Scalable Backend Development course, upgrade to CoddyKit PRO.
What will I learn in “Concurrent Work with Task and Agent”?
Run code concurrently and manage simple shared state using the Task and Agent abstractions built on top of OTP. You practise Elixir & Phoenix: Scalable Backend Development with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Elixir & Phoenix: Scalable Backend Development?
No prior experience is required. Elixir & Phoenix: Scalable Backend Development on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Concurrent Work with Task and Agent” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Elixir & Phoenix: Scalable Backend Development lesson?
Yes. Every Elixir & Phoenix: Scalable Backend Development lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Elixir Processes and Message Passing
- Implementing GenServer Behavior
- Supervisors and Application Structure
- Concurrent Work with Task and Agent