Task와 Agent를 사용한 동시 작업
OTP를 기반으로 만들어진 Task와 Agent 추상화를 사용해 코드를 동시에 실행하고 간단한 공유 상태를 관리하는 방법을 배웁니다.
Task와 Agent를 사용한 동시 작업은(는) CoddyKit의 무료 Elixir & Phoenix: Scalable Backend Development 강의입니다. 이것은 4개 중 4번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 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/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Elixir & Phoenix: Scalable Backend Development 강의 전체를 잠금 해제할 수 있습니다. Elixir & Phoenix: Scalable Backend Development 강의에는 총 4개의 강의가 포함되어 있습니다.
“Task와 Agent를 사용한 동시 작업”에서 뭘 배우나요?
OTP를 기반으로 만들어진 Task와 Agent 추상화를 사용해 코드를 동시에 실행하고 간단한 공유 상태를 관리하는 방법을 배웁니다. 브라우저에서 직접 실행하는 실습 코드로 Elixir & Phoenix: Scalable Backend Development을(를) 배우며, 24/7 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를 사용한 동시 작업