Managing program complexity with agents
Agents build on the STM, and each agent acts a lot like a reference. References allow you to coordinate multiple pieces of the state, but if you only have one piece of the state that you're updating, then that's a good use for agents. You use agents by sending them messages (functions that manipulate the agent's state) and these are run in the thread pool, although each agent only processes one task at a time.
We create agents with the agent
function, and we send messages to them with send
and send-off
. Whatever the function returns is the agent's new state value. This figure illustrates this process:
For this recipe, we'll again solve the same problem we did in the last recipe, Managing program complexity with STM.
Getting ready
We will include the same references in the project.clj
file and the same requirements in the REPL as we did in the Managing program complexity with STM recipe.
For this recipe, I'm going to use the U.S. political campaign finance...