Your SlideShare is downloading. ×
0
Concurrent programming with Agents
Concurrent programming with Agents
Concurrent programming with Agents
Concurrent programming with Agents
Concurrent programming with Agents
Concurrent programming with Agents
Concurrent programming with Agents
Concurrent programming with Agents
Concurrent programming with Agents
Concurrent programming with Agents
Concurrent programming with Agents
Concurrent programming with Agents
Concurrent programming with Agents
Concurrent programming with Agents
Concurrent programming with Agents
Concurrent programming with Agents
Concurrent programming with Agents
Concurrent programming with Agents
Concurrent programming with Agents
Concurrent programming with Agents
Concurrent programming with Agents
Concurrent programming with Agents
Concurrent programming with Agents
Concurrent programming with Agents
Concurrent programming with Agents
Concurrent programming with Agents
Concurrent programming with Agents
Concurrent programming with Agents
Concurrent programming with Agents
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Concurrent programming with Agents

2,667

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 …

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,667
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
0
Comments
0
Likes
3
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Concurrent programming with Agents Tomáš Petříček http://tomasp.net/blog http://twitter.com/tomaspetricek
  • 2. Introducing agents Inside agent’s brain Inter-agent communication Reusable agents Intelligence networks
  • 3. Sequential programs
  • 4. Task based parallelism
  • 5. Agent-based concurrency
  • 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. Example Introducing agents Simple chat room
  • 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. 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. Example Encapsulating agent into an object Exposing chat room via HTTP
  • 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. Introducing agents Inside agent’s brain Inter-agent communication Reusable agents Intelligence networks
  • 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. Example Reading statuses from Twitter Pausing the stream using blocking agent
  • 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. Introducing agents Inside agent’s brain Inter-agent communication Reusable agents Intelligence networks
  • 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. 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. 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. Introducing agents Inside agent’s brain Inter-agent communication Reusable agents Intelligence networks
  • 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. Example Bulk processing of Twitter statuses A look at the BulkingAgent implementation
  • 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. Introducing agents Inside agent’s brain Inter-agent communication Reusable agents Intelligence networks
  • 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. 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. Example Pipeline image processing
  • 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. Questions? Email: tomas@tomasp.net Blog: http://tomasp.net/blog Twitter: http://twitter.com/tomaspetricek My book: http://functional-programming.net

×