liveBook · Manning

Task

As explained in Elixir in Action, Third Edition

Task is an Elixir module and OTP abstraction for running asynchronous computations, with support for awaiting results and supervision.

Task is an OTP abstraction for running concurrent jobs that begin work immediately and terminate when the computation finishes. Unlike a GenServer, a task does not serve requests over its lifetime; it performs a one-off operation or background job. section 10.0 section 10.1

Choose the task form according to how the result and process lifetime should be handled:

  • Use Task.async/1 for an awaited task: it runs a zero-arity function in another process, returns immediately with a task struct, and sends the function result back to the caller. section 10.1.1
  • Use Task.await/1 or Task.await/2 to receive the result; the default five-second timeout raises if the response does not arrive. section 10.1.1
  • Use Task.start_link/1 for a non-awaited OTP-compliant job that sends no result message and terminates normally when its function finishes. section 10.1.2
  • Use Task.Supervisor for dynamically started work whose lifetime must be independent from the request-handling or other process that initiated it. section 10.1.3

Parallel independent queries

Map independent queries to tasks, then await each task to collect the results. This replaces manual spawn, send, and receive code while allowing the computations to proceed concurrently: `1..5 |> Enum.map(&Task.async(fn -> run_query.("query #{&1}") end)) |> Enum.map(&Task.await/1)`. section 10.1.1

Supervised periodic metrics

A module can `use Task`, define `start_link/1` with `Task.start_link(&loop/0)`, and place the module under a supervisor. The loop periodically collects memory usage and process count without requiring a GenServer or an external scheduler. section 10.1.2

iex(2)> Task.Supervisor.start_child(
          MyTaskSupervisor,
          fn ->
            IO.puts("Task started")
            Process.sleep(2000)
            IO.puts("Task stopping")
          end
        )
 
{:ok, #PID<0.118.0>}   #1
 
Task started           #2
Task stopping          #3

Shows a dynamically supervised task starting, performing delayed work, and stopping independently. section 10.1.3

How Elixir in Action, Third Edition covers it

How deep it goes
Explains task lifecycles, result messaging, timeouts, linking, and supervision through runnable examples rather than exhaustive API coverage.
Examples
Uses delayed computations, parallel queries, periodic metrics reporting, and asynchronously notified remote-service communication.
What it assumes
Assumes familiarity with processes, workers, supervisors, and supervision trees while teaching task-specific concurrency patterns.
What you can do after
Apply it via Task.async/1, Task.await/1, Task.start_link/1, and Task.Supervisor to structure concurrent OTP jobs.
How it connects
Connects Task with GenServer, Supervisor, DynamicSupervisor, supervision trees, processes, and OTP-compliant workers.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Elixir in Action, Third Edition. The section links point to the parts of the book it draws on.