• Save

Loading…

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

Like this presentation? Why not share!

Like this? Share it with your network

Share

Concurrent programming with Agents

on

  • 3,156 views

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.

Statistics

Views

Total Views
3,156
Views on SlideShare
2,660
Embed Views
496

Actions

Likes
3
Downloads
0
Comments
0

7 Embeds 496

http://tomasp.net 486
http://www.linkedin.com 4
http://web.archive.org 2
http://webcache.googleusercontent.com 1
http://translate.googleusercontent.com 1
http://localhost 1
https://www.linkedin.com 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Concurrent programming with Agents Presentation Transcript

  • 1. Concurrent programming with Agents
    Tomáš Petříček
    http://tomasp.net/bloghttp://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)
    memberx.AsyncGetContent(?timeout) =
    agent.PostAndAsyncReply(GetContent, ?timeout=timeout)
    memberx.GetContent(?timeout) =
    agent.PostAndReply(GetContent, ?timeout=timeout)
    memberx.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
    Multiple states
    Agent implements a state machine
    Pause
    Running
    Paused
    Resume
    Status
    Resume
  • 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.subscribesupports removal
    Connecting agents with asynchronous actions
    Create and start asynchronous workflow
    source.Notification|>Observable.addtarget.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 agentsI.
    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 & replymethods 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