Concurrent programming with Agents
Tomáš Petříček
http://tomasp.net/blog
http://twitter.com/tomaspetricek
Introducing agents
Inside agent’s brain
Inter-agent communication
Reusable agents
Intelligence networks
Sequential programs
Task based parallelism
Agent-based concurrency
Agent-based concurrency
Programs compose from agents
Instead of functions or objects
Agents exchange messages
Receive me...
Example
Introducing agents
Simple chat room
Functional loop with single state
Single state
Same reaction to all messages
Implemented using recursive loop
What if rea...
Hiding agent’s brain
Accessing agent directly
Exposes implementation details
Users can call wrong methods (e.g. Receive)
...
Example
Encapsulating agent into an object
Exposing chat room via HTTP
Hiding agent’s brain
 Asynchronous calls from F#
 Synchronous calls from F# (optional)
 Asynchronous calls from C# (tas...
Introducing agents
Inside agent’s brain
Inter-agent communication
Reusable agents
Intelligence networks
Inside agent’s brain
Single state
Accept and react
to all messages
Example: Twitter status agent with pause
Resume
Pause...
Example
Reading statuses from Twitter
Pausing the stream using blocking agent
State and state transitions
Accepting all messages
Asynchronously Receive and use pattern matching
Waiting for specific ...
Introducing agents
Inside agent’s brain
Inter-agent communication
Reusable agents
Intelligence networks
Inter-agent communication
Direct links between agents
Complicates reusability and reconfiguration
Decoupling using event...
Types of agent’s members
Send message to the agent
Members of type: 'T -> unit
Notifications from the agent
Exposed as e...
Connecting agents
Sending message in response to notification
Alternatively, Observable.subscribe supports removal
Conne...
Introducing agents
Inside agent’s brain
Inter-agent communication
Reusable agents
Intelligence networks
Reusable agents I.
Aggregating messages into bulks
Bulk specified number of messages
Emit bulk after timeout
Uses of bul...
Example
Bulk processing of Twitter statuses
A look at the BulkingAgent implementation
Reusable agents II.
Blocking queue with limited size
Block reader when queue is empty
Block adder when queue is full
Use...
Introducing agents
Inside agent’s brain
Inter-agent communication
Reusable agents
Intelligence networks
Managing intelligence network
 Lots of things going on!
How to keep a big picture?
Using loosely coupled connections
Age...
Pipeline processing
Values processed in multiple steps
Worker takes value, processes it, and sends it
Worker is blocked w...
Example
Pipeline image processing
Summary
Why use agent-based concurrency?
Easy to understand reactive applications
Elegant implementation of concurrent pa...
Questions?
Email: tomas@tomasp.net
Blog: http://tomasp.net/blog
Twitter: http://twitter.com/tomaspetricek
My book: http://...
Upcoming SlideShare
Loading in …5
×

Concurrent programming with Agents

2,815
-1

Published on

Talk about message passing concurrency in F# using agents. In this style of programming, we write application as a group of agents that run in parallel and communicate by sending immutable messages to each other. We’ll discuss several common patterns that you can use for structuring agent-based applications. For example, we’ll look how to implement pipeline processing and map-reduce pattern using agents.

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,815
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
0
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Concurrent programming with Agents

  1. 1. Concurrent programming with Agents Tomáš Petříček http://tomasp.net/blog http://twitter.com/tomaspetricek
  2. 2. Introducing agents Inside agent’s brain Inter-agent communication Reusable agents Intelligence networks
  3. 3. Sequential programs
  4. 4. Task based parallelism
  5. 5. Agent-based concurrency
  6. 6. Agent-based concurrency Programs compose from agents Instead of functions or objects Agents exchange messages Receive message and react Reactive system Handle inputs while running Emit results while running
  7. 7. Example Introducing agents Simple chat room
  8. 8. Functional loop with single state Single state Same reaction to all messages Implemented using recursive loop What if reaction depends on the state? What if agent cannot handle some message? Immutable state Maintained as parameter of recursive function Can use mutable collections for performance
  9. 9. Hiding agent’s brain Accessing agent directly Exposes implementation details Users can call wrong methods (e.g. Receive) Encapsulating agent Agent as a private filed Add methods for all (public) messages Expose asynchronous calls first
  10. 10. Example Encapsulating agent into an object Exposing chat room via HTTP
  11. 11. Hiding agent’s brain  Asynchronous calls from F#  Synchronous calls from F# (optional)  Asynchronous calls from C# (task based) member x.AsyncGetContent(?timeout) = agent.PostAndAsyncReply(GetContent, ?timeout=timeout) member x.GetContent(?timeout) = agent.PostAndReply(GetContent, ?timeout=timeout) member x.GetContentAsync() = Async.StartAsTask(agent.PostAndAsyncReply(GetContent)) member x.GetContentAsync(cancellationToken) = Async.StartAsTask(agent.PostAndAsyncReply(GetContent), cancellationToken=cancellationToken)
  12. 12. Introducing agents Inside agent’s brain Inter-agent communication Reusable agents Intelligence networks
  13. 13. Inside agent’s brain Single state Accept and react to all messages Example: Twitter status agent with pause Resume Pause d Running Resume Status Pause Multiple states Agent implements a state machine
  14. 14. Example Reading statuses from Twitter Pausing the stream using blocking agent
  15. 15. State and state transitions Accepting all messages Asynchronously Receive and use pattern matching Waiting for specific message Other messages stay in the queue Writing message handling using Scan let rec state = agent.Scan(function | Message -> Some <| async { handleMessage() return! newState } | _ -> None )
  16. 16. Introducing agents Inside agent’s brain Inter-agent communication Reusable agents Intelligence networks
  17. 17. Inter-agent communication Direct links between agents Complicates reusability and reconfiguration Decoupling using events Expose event instead of sending message We used events in the previous example!
  18. 18. Types of agent’s members Send message to the agent Members of type: 'T -> unit Notifications from the agent Exposed as events or observables: IObservable<'T> No synchronization and no thread guarantees Send and wait for a reply Uses asynchronous reply channels from F# library Takes arguments and returns result: 'T -> Async<'R>
  19. 19. Connecting agents Sending message in response to notification Alternatively, Observable.subscribe supports removal Connecting agents with asynchronous actions Create and start asynchronous workflow source.Notification |> Observable.add target.Action async { while true do let! value = source.AsyncAction() do! target.Action(value) } |> Async.Start
  20. 20. Introducing agents Inside agent’s brain Inter-agent communication Reusable agents Intelligence networks
  21. 21. Reusable agents I. Aggregating messages into bulks Bulk specified number of messages Emit bulk after timeout Uses of bulking agent Grouping data for further processing Writing live data to a database new BulkingAgent : int -> int -> BulkingAgent member Enqueue : 'T -> unit member BulkProduced : Event<'T[]>
  22. 22. Example Bulk processing of Twitter statuses A look at the BulkingAgent implementation
  23. 23. Reusable agents II. Blocking queue with limited size Block reader when queue is empty Block adder when queue is full Uses of blocking queue agent The producer consumer pattern Immediate buffer in pipeline processing new BlockingQueueAgent : int -> BlockingQueueAgent member AsyncGet : unit -> Async<'T> member AsyncAdd : 'T -> Async<unit>
  24. 24. Introducing agents Inside agent’s brain Inter-agent communication Reusable agents Intelligence networks
  25. 25. Managing intelligence network  Lots of things going on! How to keep a big picture? Using loosely coupled connections Agents don’t reference each other directly Common ways of organizing agents Worker agent – Single agent does all the work Layered network – Agent uses agents from lower level Pipeline processing – Step-by-step processing
  26. 26. Pipeline processing Values processed in multiple steps Worker takes value, processes it, and sends it Worker is blocked when source is empty Worker is blocked when target is full Steps of the pipeline run in parallel
  27. 27. Example Pipeline image processing
  28. 28. Summary Why use agent-based concurrency? Easy to understand reactive applications Elegant implementation of concurrent patterns How to write an agent-based application? State machine using recursive functions Encapsulate and provide communication points (send and send & reply methods and notifications) Use reusable agents for recurring patterns
  29. 29. Questions? Email: tomas@tomasp.net Blog: http://tomasp.net/blog Twitter: http://twitter.com/tomaspetricek My book: http://functional-programming.net

×