Agents are processes that maintain state.
Effectively, this is wrapping the Stack.Stash
process we’ve implemented earlier.
An Elixir Task wraps OTP message passing.
Instead of calling a process with
start_link
and then waiting for the response withreceive
, you start the process withTask.async
and thenTask.await
on the specific pid.Tasks are OTP servers and can be added to the Supervisor tree.
Task.await
cannot be used with tasks started by the Supervisor as we don’t know the pid to await.The first Agent example shows passing an initialization function vs. a value. Need to research if this is mandatory.
The phrase
&(&1)
is the identify function and used to retrieve the current state.{:ok, count } = Agent.start(fn -> 0 end) Agent.get(count, &(&1)) Agent.update(count, &(&1 + 1))
All notes and comments are my own opinion. Follow me at @rgacote@genserver.social