0Pricing
Elixir & Phoenix: Scalable Backend Development · 课时

使用 Task 与 Agent 进行并发工作

使用构建于 OTP 之上的 Task 和 Agent 抽象并发运行代码并管理简单的共享状态。

使用 Task 与 Agent 进行并发工作 是 CoddyKit 上的免费 Elixir & Phoenix: Scalable Backend Development 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Elixir & Phoenix: Scalable Backend Development 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Elixir & Phoenix: Scalable Backend Development 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

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/await for concurrent work and results
  • Task.async_stream for bounded parallel mapping
  • Agent for simple, safe shared state
  • Pick Task, Agent, or GenServer by how much control you need

常见问题解答

「使用 Task 与 Agent 进行并发工作」课时是免费的吗?

是的 — 「使用 Task 与 Agent 进行并发工作」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Elixir & Phoenix: Scalable Backend Development 课程的其余内容,请升级到 CoddyKit PRO。 Elixir & Phoenix: Scalable Backend Development 课程共包含 4 节课。

「使用 Task 与 Agent 进行并发工作」这节课中我会学到什么?

使用构建于 OTP 之上的 Task 和 Agent 抽象并发运行代码并管理简单的共享状态。 你通过在浏览器中直接运行的动手代码来练习 Elixir & Phoenix: Scalable Backend Development,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Elixir & Phoenix: Scalable Backend Development 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Elixir & Phoenix: Scalable Backend Development 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「使用 Task 与 Agent 进行并发工作」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Elixir & Phoenix: Scalable Backend Development 课中编写并运行代码吗?

能。每节 Elixir & Phoenix: Scalable Backend Development 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. Elixir 进程与消息传递
  2. 实现 GenServer 行为
  3. 监督者与应用结构
  4. 使用 Task 与 Agent 进行并发工作
← 返回 Elixir & Phoenix: Scalable Backend Development