0
Reactor Pattern
                       &

Event-Driven Programming
   A scalable concurrent approach,
   using EventMachin...
Reactor Pattern
                            &

     Event-Driven Programming
http://godfat.org/slide/2010-02-29-reactor-pa...
Table of Contents
Table of Contents
• concurrency, why and how in network
Table of Contents
• concurrency, why and how in network
• Event-Driven Programming explained in
  Flash with Ruby syntax
Table of Contents
• concurrency, why and how in network
• Event-Driven Programming explained in
  Flash with Ruby syntax

...
Table of Contents
• concurrency, why and how in network
• Event-Driven Programming explained in
  Flash with Ruby syntax

...
Table of Contents
• concurrency, why and how in network
• Event-Driven Programming explained in
  Flash with Ruby syntax

...
Table of Contents
• concurrency, why and how in network
• Event-Driven Programming explained in
  Flash with Ruby syntax

...
concurrency, why
and how in network
concurrency, why
  and how in network
• [network I/O] is slow, we shouldn't wait for
  [network I/O] while make [CPU] idle.
concurrency, why
  and how in network
• [network I/O] is slow, we shouldn't wait for
  [network I/O] while make [CPU] idle...
concurrency, why
  and how in network
• [network I/O] is slow, we shouldn't wait for
  [network I/O] while make [CPU] idle...
Table of Contents
• concurrency, why and how in network
• Event-Driven Programming explained in
  Flash with Ruby syntax

...
Event-Driven Programming
         to the rescue
Event-Driven Programming

 • only one process/thread
Event-Driven Programming

 • only one process/thread
 • inversion of control
Event-Driven Programming

 • only one process/thread
 • inversion of control
 • consists of an event loop and various
   e...
Event-Driven Programming


 • inversion of control
Event-Driven Programming
loop{
  # you control the flow
  do_something
}
       • inversion of control
Event-Driven Programming
                            register method(:do_something)
loop{                       loop{
  # ...
Event-Driven Programming
                        register method(:do_something)
                        loop{
            ...
Event-Driven Programming
                        register method(:do_something)
                        loop{
            ...
Event-Driven Programming
                        register method(:do_something)
                        loop{
            ...
Event-Driven Programming
 in Flash with Ruby syntax
Event-Driven Programming
 in Flash with Ruby syntax
  • game loop, an example of event loop
Event-Driven Programming
 in Flash with Ruby syntax
  • game loop, an example of event loop
  • Flash ActionScript, onEnte...
Event-Driven Programming
 in Flash with Ruby syntax
  • game loop, an example of event loop
  • Flash ActionScript, onEnte...
Event-Driven Programming
 in Flash with Ruby syntax

# in each sprite thread
30.times{
  application.draw sprite
  sprite....
sprite.onEnterFrame = lambda{
  sprite.x += 1
}




# in each sprite thread
30.times{
  application.draw sprite
  sprite.x...
sprite.onEnterFrame = lambda{
  sprite.x += 1
}
application.register sprite
30.times{ # event loop, also called game loop
...
Table of Contents
• concurrency, why and how in network
• Event-Driven Programming explained in
  Flash with Ruby syntax

...
Reactor Pattern
Reactor Pattern
loop{
  data = read
  handle data
}
Reactor Pattern
                register method(:handle)
loop{           loop{
  data = read     data = partial_read
  han...
Event-Driven Programming
                        register method(:do_something)
loop{                   loop{
  # you cont...
Reactor Pattern
                register method(:handle)
loop{           loop{
  data = read     data = partial_read
  han...
Reactor Pattern




    by wikipedia
Reactor Pattern
• resources # e.g. network I/O




                by wikipedia
Reactor Pattern
• resources # e.g. network I/O
• synchronous event demultiplexer
  # i.e. the blocking event loop




    ...
Reactor Pattern
• resources # e.g. network I/O
• synchronous event demultiplexer
  # i.e. the blocking event loop

• dispa...
Reactor Pattern
• resources # e.g. network I/O
• synchronous event demultiplexer
  # i.e. the blocking event loop

• dispa...
Reactor Pattern
Request
(resource)
Reactor Pattern
                  EventMachine
Request
                  (demultiplexer
(resource)
                   + di...
Reactor Pattern
                  EventMachine
Request                            Thin (or AMQP)
                  (demult...
Reactor Pattern
                  EventMachine
Request                                Thin (or AMQP)
                  (de...
Reactor Pattern
                     EventMachine
Request                                         Thin (or AMQP)
         ...
Reactor Pattern
                             EventMachine
        Request                                         Thin (or...
your rails
application
                        Reactor Pattern
                                EventMachine
           Req...
Reactor Pattern
EventMachine is a generic network I/O server/client
library due to I/O and request handler separation in
 ...
Reactor Pattern
• EventMachine (Ruby)
Reactor Pattern
• EventMachine (Ruby)
• Twisted (Python)
Reactor Pattern
• EventMachine (Ruby)
• Twisted (Python)
• nodejs (JavaScript in V8)
Reactor Pattern
• EventMachine (Ruby)
• Twisted (Python)
• nodejs (JavaScript in V8)
• libevent and libev (C)
Reactor Pattern
• select (POSIX)
Reactor Pattern
• select (POSIX)
• poll (POSIX)
Reactor Pattern
• select (POSIX)
• poll (POSIX)
• epoll (Linux)
Reactor Pattern
• select (POSIX)
• poll (POSIX)
• epoll (Linux)
• kqueue (BSD, Mac OS X (Darwin))
Table of Contents
• concurrency, why and how in network
• Event-Driven Programming explained in
  Flash with Ruby syntax

...
how Thin works
• Thin::Server
how Thin works
• Thin::Server
• Thin::Backends::TcpServer
 # communicate with EventMachine
how Thin works
• Thin::Server
• Thin::Backends::TcpServer
 # communicate with EventMachine

• Thin::Connection
 # EventMac...
how Thin works
• Thin::Server
• Thin::Backends::TcpServer
 # communicate with EventMachine

• Thin::Connection
 # EventMac...
how Thin works
Sorry! To be continued......
how Thin works
Sorry! To be continued......


           ?
Upcoming SlideShare
Loading in...5
×

2010-02-09 Reactor Pattern & Event Driven Programming

6,690

Published on

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

No Downloads
Views
Total Views
6,690
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
121
Comments
0
Likes
6
Embeds 0
No embeds

No notes for slide

Transcript of "2010-02-09 Reactor Pattern & Event Driven Programming"

  1. 1. Reactor Pattern & Event-Driven Programming A scalable concurrent approach, using EventMachine with Thin as an example Lin Jen-Shin, http://godfat.org/
  2. 2. Reactor Pattern & Event-Driven Programming http://godfat.org/slide/2010-02-29-reactor-pattern-and.pdf Lin Jen-Shin, http://godfat.org/
  3. 3. Table of Contents
  4. 4. Table of Contents • concurrency, why and how in network
  5. 5. Table of Contents • concurrency, why and how in network • Event-Driven Programming explained in Flash with Ruby syntax
  6. 6. Table of Contents • concurrency, why and how in network • Event-Driven Programming explained in Flash with Ruby syntax • Reactor Pattern in EventMachine with Thin
  7. 7. Table of Contents • concurrency, why and how in network • Event-Driven Programming explained in Flash with Ruby syntax • Reactor Pattern in EventMachine with Thin • how Thin works
  8. 8. Table of Contents • concurrency, why and how in network • Event-Driven Programming explained in Flash with Ruby syntax • Reactor Pattern in EventMachine with Thin • how Thin works • how EventMachine works
  9. 9. Table of Contents • concurrency, why and how in network • Event-Driven Programming explained in Flash with Ruby syntax • Reactor Pattern in EventMachine with Thin • how Thin works • how EventMachine works
  10. 10. concurrency, why and how in network
  11. 11. concurrency, why and how in network • [network I/O] is slow, we shouldn't wait for [network I/O] while make [CPU] idle.
  12. 12. concurrency, why and how in network • [network I/O] is slow, we shouldn't wait for [network I/O] while make [CPU] idle. • you can replace [network I/O] and [CPU] with all other resources like [disc I/O], [memory I/O], etc.
  13. 13. concurrency, why and how in network • [network I/O] is slow, we shouldn't wait for [network I/O] while make [CPU] idle. • you can replace [network I/O] and [CPU] with all other resources like [disc I/O], [memory I/O], etc. • each kernel process/thread for each client using a blocking I/O is easy to write but not scalable at all
  14. 14. Table of Contents • concurrency, why and how in network • Event-Driven Programming explained in Flash with Ruby syntax • Reactor Pattern in EventMachine with Thin • how Thin works • how EventMachine works
  15. 15. Event-Driven Programming to the rescue
  16. 16. Event-Driven Programming • only one process/thread
  17. 17. Event-Driven Programming • only one process/thread • inversion of control
  18. 18. Event-Driven Programming • only one process/thread • inversion of control • consists of an event loop and various event handlers
  19. 19. Event-Driven Programming • inversion of control
  20. 20. Event-Driven Programming loop{ # you control the flow do_something } • inversion of control
  21. 21. Event-Driven Programming register method(:do_something) loop{ loop{ # you control the flow # event loop control the flow, do_something # later it calls your callback } event = pop_event_queue • inversion of control dispatch event if event }
  22. 22. Event-Driven Programming register method(:do_something) loop{ # event loop control the flow, # later it calls your callback event = pop_event_queue dispatch event if event } • consists of an event loop and various event handlers
  23. 23. Event-Driven Programming register method(:do_something) loop{ # event loop control the flow, # later it calls your callback event = pop_event_queue dispatch event if event } • consists of an event loop and various event handlers
  24. 24. Event-Driven Programming register method(:do_something) loop{ # event loop control the flow, # later it calls your callback event = pop_event_queue dispatch event if event } • consists of an event loop and various event handlers
  25. 25. Event-Driven Programming in Flash with Ruby syntax
  26. 26. Event-Driven Programming in Flash with Ruby syntax • game loop, an example of event loop
  27. 27. Event-Driven Programming in Flash with Ruby syntax • game loop, an example of event loop • Flash ActionScript, onEnterFrame
  28. 28. Event-Driven Programming in Flash with Ruby syntax • game loop, an example of event loop • Flash ActionScript, onEnterFrame • frame by frame
  29. 29. Event-Driven Programming in Flash with Ruby syntax # in each sprite thread 30.times{ application.draw sprite sprite.x += 1 }
  30. 30. sprite.onEnterFrame = lambda{ sprite.x += 1 } # in each sprite thread 30.times{ application.draw sprite sprite.x += 1 }
  31. 31. sprite.onEnterFrame = lambda{ sprite.x += 1 } application.register sprite 30.times{ # event loop, also called game loop events = application.pop_event_queue events.each{ |event| application.dispatch event } # model/view separation application.draw application.sprites } # in each sprite thread 30.times{ application.draw sprite sprite.x += 1 }
  32. 32. Table of Contents • concurrency, why and how in network • Event-Driven Programming explained in Flash with Ruby syntax • Reactor Pattern in EventMachine with Thin • how Thin works • how EventMachine works
  33. 33. Reactor Pattern
  34. 34. Reactor Pattern loop{ data = read handle data }
  35. 35. Reactor Pattern register method(:handle) loop{ loop{ data = read data = partial_read handle data event = process data } dispatch event if event }
  36. 36. Event-Driven Programming register method(:do_something) loop{ loop{ # you control the flow # event loop control the flow, do_something # later it calls your callback } event = pop_event_queue dispatch event if event }
  37. 37. Reactor Pattern register method(:handle) loop{ loop{ data = read data = partial_read handle data event = process data } dispatch event if event }
  38. 38. Reactor Pattern by wikipedia
  39. 39. Reactor Pattern • resources # e.g. network I/O by wikipedia
  40. 40. Reactor Pattern • resources # e.g. network I/O • synchronous event demultiplexer # i.e. the blocking event loop by wikipedia
  41. 41. Reactor Pattern • resources # e.g. network I/O • synchronous event demultiplexer # i.e. the blocking event loop • dispatcher # i.e. handler manager and event dispatcher by wikipedia
  42. 42. Reactor Pattern • resources # e.g. network I/O • synchronous event demultiplexer # i.e. the blocking event loop • dispatcher # i.e. handler manager and event dispatcher • request handler # e.g. thin handler by wikipedia
  43. 43. Reactor Pattern Request (resource)
  44. 44. Reactor Pattern EventMachine Request (demultiplexer (resource) + dispatcher)
  45. 45. Reactor Pattern EventMachine Request Thin (or AMQP) (demultiplexer (resource) (request handler) + dispatcher)
  46. 46. Reactor Pattern EventMachine Request Thin (or AMQP) (demultiplexer (resource) (request handler) + dispatcher) Rack Thin handler
  47. 47. Reactor Pattern EventMachine Request Thin (or AMQP) (demultiplexer (resource) (request handler) + dispatcher) Rack Rails Rack Thin rack env adapter handler
  48. 48. Reactor Pattern EventMachine Request Thin (or AMQP) (demultiplexer (resource) (request handler) + dispatcher) Rack Rails Rack Thin Rails rack env adapter handler
  49. 49. your rails application Reactor Pattern EventMachine Request Thin (or AMQP) (demultiplexer (resource) (request handler) + dispatcher) Rack Rails Rack Thin Rails rack env adapter handler
  50. 50. Reactor Pattern EventMachine is a generic network I/O server/client library due to I/O and request handler separation in Reactor Pattern
  51. 51. Reactor Pattern • EventMachine (Ruby)
  52. 52. Reactor Pattern • EventMachine (Ruby) • Twisted (Python)
  53. 53. Reactor Pattern • EventMachine (Ruby) • Twisted (Python) • nodejs (JavaScript in V8)
  54. 54. Reactor Pattern • EventMachine (Ruby) • Twisted (Python) • nodejs (JavaScript in V8) • libevent and libev (C)
  55. 55. Reactor Pattern • select (POSIX)
  56. 56. Reactor Pattern • select (POSIX) • poll (POSIX)
  57. 57. Reactor Pattern • select (POSIX) • poll (POSIX) • epoll (Linux)
  58. 58. Reactor Pattern • select (POSIX) • poll (POSIX) • epoll (Linux) • kqueue (BSD, Mac OS X (Darwin))
  59. 59. Table of Contents • concurrency, why and how in network • Event-Driven Programming explained in Flash with Ruby syntax • Reactor Pattern in EventMachine with Thin • how Thin works • how EventMachine works
  60. 60. how Thin works • Thin::Server
  61. 61. how Thin works • Thin::Server • Thin::Backends::TcpServer # communicate with EventMachine
  62. 62. how Thin works • Thin::Server • Thin::Backends::TcpServer # communicate with EventMachine • Thin::Connection # EventMachine event handler
  63. 63. how Thin works • Thin::Server • Thin::Backends::TcpServer # communicate with EventMachine • Thin::Connection # EventMachine event handler • Thin::Request # partial HTTP request parsing # Rack env builder
  64. 64. how Thin works Sorry! To be continued......
  65. 65. how Thin works Sorry! To be continued...... ?
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×