concept agent in category .net

This is an excerpt from Manning's book Concurrency in .NET.
Knowledge of concurrent programming models that adopt the functional paradigm (functional, asynchronous, event-driven, and message passing with agents and actors)
You’ll use the agent-based concurrent programming style, which relies on message passing as a vehicle to communicate between small units of computations called agents. Each agent may own an internal state, with single-threaded access to guarantee thread safety without the need of any lock (or other any other synchronization primitive). Because agents are easy to understand, programming with them is an effective tool for building scalable and responsive applications that ease the implementation of advanced asynchronous logic.
State—Agents can have an internal state that’s isolated and never shared, so they never need to compete for locks to be accessed. Message—Agents can communicate only through messages, which are sent asynchronously and are buffered in a mailbox. Figure 11.4 An agent consists of a mailbox that queues the income messages, a state, and a behavior that runs in a loop, which processes one message at a time. The behavior is the functionality applied to the messages.
![]()
The following listing is the same version of the agent that counts its messages (shown in Listing 11.1), but this time it uses a recursive asynchronous function that maintains a state.
Listing 11.2 Simple
MailboxProcessorwith a recursive functionlet agent = Agent<string>.Start(fun inbox -> let rec loop count = async { #1 let! message = inbox.Receive() use client = new WebClient() let uri = Uri message let! site = client.AsyncDownloadString(uri) printfn "Size of %s is %d - total messages %d" uri.Host ➥ site.Length (count + 1) return! loop (count + 1) } #2 loop 0) agent.Post "http://www.google.com" agent.Post "http://www.microsoft.com" #1 Uses an asynchronous recursive function that maintains a state in an immutable manner #2 The recursive function is a tail call, asynchronously passing an updated state.