TaskとAgentによる並行処理
OTP上に構築されたTaskとAgentの抽象化を使い、コードを並行実行して単純な共有状態を管理します。
「TaskとAgentによる並行処理」はCoddyKit上の無料Elixir & Phoenix: Scalable Backend Developmentレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応の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/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
よくある質問
「TaskとAgentによる並行処理」レッスンは無料ですか?
はい。「TaskとAgentによる並行処理」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Elixir & Phoenix: Scalable Backend Developmentコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Elixir & Phoenix: Scalable Backend Developmentコースには全4レッスンが含まれています。
「TaskとAgentによる並行処理」で何を学びますか?
OTP上に構築されたTaskとAgentの抽象化を使い、コードを並行実行して単純な共有状態を管理します。 ブラウザで直接実行するハンズオンコードでElixir & Phoenix: Scalable Backend Developmentを演習し、24時間対応の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フィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- Elixirのプロセスとメッセージパッシング
- GenServerビヘイビアの実装
- スーパーバイザーとアプリケーション構造
- TaskとAgentによる並行処理