2010-02-09 Reactor Pattern & Event Driven Programming
-
Upload
lin-jen-shin -
Category
Technology
-
view
111 -
download
2
description
Transcript of 2010-02-09 Reactor Pattern & Event Driven Programming
![Page 1: 2010-02-09 Reactor Pattern & Event Driven Programming](https://reader034.fdocuments.net/reader034/viewer/2022052618/54c8c7314a79591b0f8b45cd/html5/thumbnails/1.jpg)
Reactor Pattern&
Event-Driven ProgrammingA scalable concurrent approach,using EventMachine with Thin as an example
Lin Jen-Shin, http://godfat.org/
![Page 2: 2010-02-09 Reactor Pattern & Event Driven Programming](https://reader034.fdocuments.net/reader034/viewer/2022052618/54c8c7314a79591b0f8b45cd/html5/thumbnails/2.jpg)
Reactor Pattern&
Event-Driven Programminghttp://godfat.org/slide/2010-02-29-reactor-pattern-and.pdf
Lin Jen-Shin, http://godfat.org/
![Page 3: 2010-02-09 Reactor Pattern & Event Driven Programming](https://reader034.fdocuments.net/reader034/viewer/2022052618/54c8c7314a79591b0f8b45cd/html5/thumbnails/3.jpg)
Table of Contents
![Page 4: 2010-02-09 Reactor Pattern & Event Driven Programming](https://reader034.fdocuments.net/reader034/viewer/2022052618/54c8c7314a79591b0f8b45cd/html5/thumbnails/4.jpg)
Table of Contents
•concurrency, why and how in network
![Page 5: 2010-02-09 Reactor Pattern & Event Driven Programming](https://reader034.fdocuments.net/reader034/viewer/2022052618/54c8c7314a79591b0f8b45cd/html5/thumbnails/5.jpg)
Table of Contents
•concurrency, why and how in network
•Event-Driven Programming explained in Flash with Ruby syntax
![Page 6: 2010-02-09 Reactor Pattern & Event Driven Programming](https://reader034.fdocuments.net/reader034/viewer/2022052618/54c8c7314a79591b0f8b45cd/html5/thumbnails/6.jpg)
Table of Contents
•concurrency, why and how in network
•Event-Driven Programming explained in Flash with Ruby syntax
•Reactor Pattern in EventMachine with Thin
![Page 7: 2010-02-09 Reactor Pattern & Event Driven Programming](https://reader034.fdocuments.net/reader034/viewer/2022052618/54c8c7314a79591b0f8b45cd/html5/thumbnails/7.jpg)
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
![Page 8: 2010-02-09 Reactor Pattern & Event Driven Programming](https://reader034.fdocuments.net/reader034/viewer/2022052618/54c8c7314a79591b0f8b45cd/html5/thumbnails/8.jpg)
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
![Page 9: 2010-02-09 Reactor Pattern & Event Driven Programming](https://reader034.fdocuments.net/reader034/viewer/2022052618/54c8c7314a79591b0f8b45cd/html5/thumbnails/9.jpg)
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
![Page 10: 2010-02-09 Reactor Pattern & Event Driven Programming](https://reader034.fdocuments.net/reader034/viewer/2022052618/54c8c7314a79591b0f8b45cd/html5/thumbnails/10.jpg)
concurrency, whyand how in network
![Page 11: 2010-02-09 Reactor Pattern & Event Driven Programming](https://reader034.fdocuments.net/reader034/viewer/2022052618/54c8c7314a79591b0f8b45cd/html5/thumbnails/11.jpg)
concurrency, whyand how in network
• [network I/O] is slow, we shouldn't wait for [network I/O] while make [CPU] idle.
![Page 12: 2010-02-09 Reactor Pattern & Event Driven Programming](https://reader034.fdocuments.net/reader034/viewer/2022052618/54c8c7314a79591b0f8b45cd/html5/thumbnails/12.jpg)
concurrency, whyand 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.
![Page 13: 2010-02-09 Reactor Pattern & Event Driven Programming](https://reader034.fdocuments.net/reader034/viewer/2022052618/54c8c7314a79591b0f8b45cd/html5/thumbnails/13.jpg)
concurrency, whyand 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
![Page 14: 2010-02-09 Reactor Pattern & Event Driven Programming](https://reader034.fdocuments.net/reader034/viewer/2022052618/54c8c7314a79591b0f8b45cd/html5/thumbnails/14.jpg)
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
![Page 15: 2010-02-09 Reactor Pattern & Event Driven Programming](https://reader034.fdocuments.net/reader034/viewer/2022052618/54c8c7314a79591b0f8b45cd/html5/thumbnails/15.jpg)
Event-Driven Programmingto the rescue
![Page 16: 2010-02-09 Reactor Pattern & Event Driven Programming](https://reader034.fdocuments.net/reader034/viewer/2022052618/54c8c7314a79591b0f8b45cd/html5/thumbnails/16.jpg)
Event-Driven Programming
•only one process/thread
![Page 17: 2010-02-09 Reactor Pattern & Event Driven Programming](https://reader034.fdocuments.net/reader034/viewer/2022052618/54c8c7314a79591b0f8b45cd/html5/thumbnails/17.jpg)
Event-Driven Programming
•only one process/thread
• inversion of control
![Page 18: 2010-02-09 Reactor Pattern & Event Driven Programming](https://reader034.fdocuments.net/reader034/viewer/2022052618/54c8c7314a79591b0f8b45cd/html5/thumbnails/18.jpg)
Event-Driven Programming
•only one process/thread
• inversion of control
•consists of an event loop and variousevent handlers
![Page 19: 2010-02-09 Reactor Pattern & Event Driven Programming](https://reader034.fdocuments.net/reader034/viewer/2022052618/54c8c7314a79591b0f8b45cd/html5/thumbnails/19.jpg)
Event-Driven Programming
• inversion of control
![Page 20: 2010-02-09 Reactor Pattern & Event Driven Programming](https://reader034.fdocuments.net/reader034/viewer/2022052618/54c8c7314a79591b0f8b45cd/html5/thumbnails/20.jpg)
Event-Driven Programmingloop{ # you control the flow do_something}
• inversion of control
![Page 21: 2010-02-09 Reactor Pattern & Event Driven Programming](https://reader034.fdocuments.net/reader034/viewer/2022052618/54c8c7314a79591b0f8b45cd/html5/thumbnails/21.jpg)
Event-Driven Programmingloop{ # you control the flow do_something}
• inversion of control
register method(:do_something)loop{ # event loop control the flow, # later it calls your callback event = pop_event_queue dispatch event if event}
![Page 22: 2010-02-09 Reactor Pattern & Event Driven Programming](https://reader034.fdocuments.net/reader034/viewer/2022052618/54c8c7314a79591b0f8b45cd/html5/thumbnails/22.jpg)
•consists of an event loop and variousevent handlers
Event-Driven Programmingregister method(:do_something)loop{ # event loop control the flow, # later it calls your callback event = pop_event_queue dispatch event if event}
![Page 23: 2010-02-09 Reactor Pattern & Event Driven Programming](https://reader034.fdocuments.net/reader034/viewer/2022052618/54c8c7314a79591b0f8b45cd/html5/thumbnails/23.jpg)
•consists of an event loop and variousevent handlers
Event-Driven Programmingregister method(:do_something)loop{ # event loop control the flow, # later it calls your callback event = pop_event_queue dispatch event if event}
![Page 24: 2010-02-09 Reactor Pattern & Event Driven Programming](https://reader034.fdocuments.net/reader034/viewer/2022052618/54c8c7314a79591b0f8b45cd/html5/thumbnails/24.jpg)
•consists of an event loop and variousevent handlers
Event-Driven Programmingregister method(:do_something)loop{ # event loop control the flow, # later it calls your callback event = pop_event_queue dispatch event if event}
![Page 25: 2010-02-09 Reactor Pattern & Event Driven Programming](https://reader034.fdocuments.net/reader034/viewer/2022052618/54c8c7314a79591b0f8b45cd/html5/thumbnails/25.jpg)
Event-Driven Programmingin Flash with Ruby syntax
![Page 26: 2010-02-09 Reactor Pattern & Event Driven Programming](https://reader034.fdocuments.net/reader034/viewer/2022052618/54c8c7314a79591b0f8b45cd/html5/thumbnails/26.jpg)
Event-Driven Programmingin Flash with Ruby syntax•game loop, an example of event loop
![Page 27: 2010-02-09 Reactor Pattern & Event Driven Programming](https://reader034.fdocuments.net/reader034/viewer/2022052618/54c8c7314a79591b0f8b45cd/html5/thumbnails/27.jpg)
Event-Driven Programmingin Flash with Ruby syntax•game loop, an example of event loop
•Flash ActionScript, onEnterFrame
![Page 28: 2010-02-09 Reactor Pattern & Event Driven Programming](https://reader034.fdocuments.net/reader034/viewer/2022052618/54c8c7314a79591b0f8b45cd/html5/thumbnails/28.jpg)
Event-Driven Programmingin Flash with Ruby syntax•game loop, an example of event loop
•Flash ActionScript, onEnterFrame
• frame by frame
![Page 29: 2010-02-09 Reactor Pattern & Event Driven Programming](https://reader034.fdocuments.net/reader034/viewer/2022052618/54c8c7314a79591b0f8b45cd/html5/thumbnails/29.jpg)
Event-Driven Programmingin Flash with Ruby syntax
# in each sprite thread30.times{ application.draw sprite sprite.x += 1}
![Page 30: 2010-02-09 Reactor Pattern & Event Driven Programming](https://reader034.fdocuments.net/reader034/viewer/2022052618/54c8c7314a79591b0f8b45cd/html5/thumbnails/30.jpg)
# in each sprite thread30.times{ application.draw sprite sprite.x += 1}
sprite.onEnterFrame = lambda{ sprite.x += 1}
![Page 31: 2010-02-09 Reactor Pattern & Event Driven Programming](https://reader034.fdocuments.net/reader034/viewer/2022052618/54c8c7314a79591b0f8b45cd/html5/thumbnails/31.jpg)
application.register sprite30.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 thread30.times{ application.draw sprite sprite.x += 1}
sprite.onEnterFrame = lambda{ sprite.x += 1}
![Page 32: 2010-02-09 Reactor Pattern & Event Driven Programming](https://reader034.fdocuments.net/reader034/viewer/2022052618/54c8c7314a79591b0f8b45cd/html5/thumbnails/32.jpg)
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
![Page 33: 2010-02-09 Reactor Pattern & Event Driven Programming](https://reader034.fdocuments.net/reader034/viewer/2022052618/54c8c7314a79591b0f8b45cd/html5/thumbnails/33.jpg)
Reactor Pattern
![Page 34: 2010-02-09 Reactor Pattern & Event Driven Programming](https://reader034.fdocuments.net/reader034/viewer/2022052618/54c8c7314a79591b0f8b45cd/html5/thumbnails/34.jpg)
Reactor Patternloop{ data = read handle data}
![Page 35: 2010-02-09 Reactor Pattern & Event Driven Programming](https://reader034.fdocuments.net/reader034/viewer/2022052618/54c8c7314a79591b0f8b45cd/html5/thumbnails/35.jpg)
Reactor Patternloop{ data = read handle data}
register method(:handle)loop{ data = partial_read event = process data dispatch event if event}
![Page 36: 2010-02-09 Reactor Pattern & Event Driven Programming](https://reader034.fdocuments.net/reader034/viewer/2022052618/54c8c7314a79591b0f8b45cd/html5/thumbnails/36.jpg)
Event-Driven Programmingloop{ # you control the flow do_something}
register method(:do_something)loop{ # event loop control the flow, # later it calls your callback event = pop_event_queue dispatch event if event}
![Page 37: 2010-02-09 Reactor Pattern & Event Driven Programming](https://reader034.fdocuments.net/reader034/viewer/2022052618/54c8c7314a79591b0f8b45cd/html5/thumbnails/37.jpg)
Reactor Patternloop{ data = read handle data}
register method(:handle)loop{ data = partial_read event = process data dispatch event if event}
![Page 38: 2010-02-09 Reactor Pattern & Event Driven Programming](https://reader034.fdocuments.net/reader034/viewer/2022052618/54c8c7314a79591b0f8b45cd/html5/thumbnails/38.jpg)
Reactor Pattern
by wikipedia
![Page 39: 2010-02-09 Reactor Pattern & Event Driven Programming](https://reader034.fdocuments.net/reader034/viewer/2022052618/54c8c7314a79591b0f8b45cd/html5/thumbnails/39.jpg)
• resources # e.g. network I/O
Reactor Pattern
by wikipedia
![Page 40: 2010-02-09 Reactor Pattern & Event Driven Programming](https://reader034.fdocuments.net/reader034/viewer/2022052618/54c8c7314a79591b0f8b45cd/html5/thumbnails/40.jpg)
• resources # e.g. network I/O
• synchronous event demultiplexer# i.e. the blocking event loop
Reactor Pattern
by wikipedia
![Page 41: 2010-02-09 Reactor Pattern & Event Driven Programming](https://reader034.fdocuments.net/reader034/viewer/2022052618/54c8c7314a79591b0f8b45cd/html5/thumbnails/41.jpg)
• resources # e.g. network I/O
• synchronous event demultiplexer# i.e. the blocking event loop
•dispatcher# i.e. handler manager and event dispatcher
Reactor Pattern
by wikipedia
![Page 42: 2010-02-09 Reactor Pattern & Event Driven Programming](https://reader034.fdocuments.net/reader034/viewer/2022052618/54c8c7314a79591b0f8b45cd/html5/thumbnails/42.jpg)
• 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
Reactor Pattern
by wikipedia
![Page 43: 2010-02-09 Reactor Pattern & Event Driven Programming](https://reader034.fdocuments.net/reader034/viewer/2022052618/54c8c7314a79591b0f8b45cd/html5/thumbnails/43.jpg)
Reactor PatternRequest
(resource)
![Page 44: 2010-02-09 Reactor Pattern & Event Driven Programming](https://reader034.fdocuments.net/reader034/viewer/2022052618/54c8c7314a79591b0f8b45cd/html5/thumbnails/44.jpg)
Reactor PatternRequest
(resource)EventMachine (demultiplexer + dispatcher)
![Page 45: 2010-02-09 Reactor Pattern & Event Driven Programming](https://reader034.fdocuments.net/reader034/viewer/2022052618/54c8c7314a79591b0f8b45cd/html5/thumbnails/45.jpg)
Reactor PatternRequest
(resource)Thin (or AMQP)(request handler)
EventMachine (demultiplexer + dispatcher)
![Page 46: 2010-02-09 Reactor Pattern & Event Driven Programming](https://reader034.fdocuments.net/reader034/viewer/2022052618/54c8c7314a79591b0f8b45cd/html5/thumbnails/46.jpg)
Reactor PatternRequest
(resource)Thin (or AMQP)(request handler)
Rack Thin handler
EventMachine (demultiplexer + dispatcher)
![Page 47: 2010-02-09 Reactor Pattern & Event Driven Programming](https://reader034.fdocuments.net/reader034/viewer/2022052618/54c8c7314a79591b0f8b45cd/html5/thumbnails/47.jpg)
Reactor PatternRequest
(resource)Thin (or AMQP)(request handler)
Rack Thin handler
EventMachine (demultiplexer + dispatcher)
Rack Rails adapter rack env
![Page 48: 2010-02-09 Reactor Pattern & Event Driven Programming](https://reader034.fdocuments.net/reader034/viewer/2022052618/54c8c7314a79591b0f8b45cd/html5/thumbnails/48.jpg)
Reactor PatternRequest
(resource)Thin (or AMQP)(request handler)
Rack Thin handler
EventMachine (demultiplexer + dispatcher)
Rails Rack Rails adapter rack env
![Page 49: 2010-02-09 Reactor Pattern & Event Driven Programming](https://reader034.fdocuments.net/reader034/viewer/2022052618/54c8c7314a79591b0f8b45cd/html5/thumbnails/49.jpg)
Reactor PatternRequest
(resource)Thin (or AMQP)(request handler)
Rack Thin handler
EventMachine (demultiplexer + dispatcher)
Rails
your rails application
Rack Rails adapter rack env
![Page 50: 2010-02-09 Reactor Pattern & Event Driven Programming](https://reader034.fdocuments.net/reader034/viewer/2022052618/54c8c7314a79591b0f8b45cd/html5/thumbnails/50.jpg)
Reactor PatternEventMachine is a generic network I/O server/clientlibrary due to I/O and request handler separation in
Reactor Pattern
![Page 51: 2010-02-09 Reactor Pattern & Event Driven Programming](https://reader034.fdocuments.net/reader034/viewer/2022052618/54c8c7314a79591b0f8b45cd/html5/thumbnails/51.jpg)
•EventMachine (Ruby)
Reactor Pattern
![Page 52: 2010-02-09 Reactor Pattern & Event Driven Programming](https://reader034.fdocuments.net/reader034/viewer/2022052618/54c8c7314a79591b0f8b45cd/html5/thumbnails/52.jpg)
•EventMachine (Ruby)
•Twisted (Python)
Reactor Pattern
![Page 53: 2010-02-09 Reactor Pattern & Event Driven Programming](https://reader034.fdocuments.net/reader034/viewer/2022052618/54c8c7314a79591b0f8b45cd/html5/thumbnails/53.jpg)
•EventMachine (Ruby)
•Twisted (Python)
•nodejs (JavaScript in V8)
Reactor Pattern
![Page 54: 2010-02-09 Reactor Pattern & Event Driven Programming](https://reader034.fdocuments.net/reader034/viewer/2022052618/54c8c7314a79591b0f8b45cd/html5/thumbnails/54.jpg)
•EventMachine (Ruby)
•Twisted (Python)
•nodejs (JavaScript in V8)
• libevent and libev (C)
Reactor Pattern
![Page 55: 2010-02-09 Reactor Pattern & Event Driven Programming](https://reader034.fdocuments.net/reader034/viewer/2022052618/54c8c7314a79591b0f8b45cd/html5/thumbnails/55.jpg)
• select (POSIX)
Reactor Pattern
![Page 56: 2010-02-09 Reactor Pattern & Event Driven Programming](https://reader034.fdocuments.net/reader034/viewer/2022052618/54c8c7314a79591b0f8b45cd/html5/thumbnails/56.jpg)
• select (POSIX)
•poll (POSIX)
Reactor Pattern
![Page 57: 2010-02-09 Reactor Pattern & Event Driven Programming](https://reader034.fdocuments.net/reader034/viewer/2022052618/54c8c7314a79591b0f8b45cd/html5/thumbnails/57.jpg)
• select (POSIX)
•poll (POSIX)
•epoll (Linux)
Reactor Pattern
![Page 58: 2010-02-09 Reactor Pattern & Event Driven Programming](https://reader034.fdocuments.net/reader034/viewer/2022052618/54c8c7314a79591b0f8b45cd/html5/thumbnails/58.jpg)
• select (POSIX)
•poll (POSIX)
•epoll (Linux)
•kqueue (BSD, Mac OS X (Darwin))
Reactor Pattern
![Page 59: 2010-02-09 Reactor Pattern & Event Driven Programming](https://reader034.fdocuments.net/reader034/viewer/2022052618/54c8c7314a79591b0f8b45cd/html5/thumbnails/59.jpg)
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
![Page 60: 2010-02-09 Reactor Pattern & Event Driven Programming](https://reader034.fdocuments.net/reader034/viewer/2022052618/54c8c7314a79591b0f8b45cd/html5/thumbnails/60.jpg)
how Thin works•Thin::Server
![Page 61: 2010-02-09 Reactor Pattern & Event Driven Programming](https://reader034.fdocuments.net/reader034/viewer/2022052618/54c8c7314a79591b0f8b45cd/html5/thumbnails/61.jpg)
how Thin works•Thin::Server
•Thin::Backends::TcpServer# communicate with EventMachine
![Page 62: 2010-02-09 Reactor Pattern & Event Driven Programming](https://reader034.fdocuments.net/reader034/viewer/2022052618/54c8c7314a79591b0f8b45cd/html5/thumbnails/62.jpg)
how Thin works•Thin::Server
•Thin::Backends::TcpServer# communicate with EventMachine
•Thin::Connection# EventMachine event handler
![Page 63: 2010-02-09 Reactor Pattern & Event Driven Programming](https://reader034.fdocuments.net/reader034/viewer/2022052618/54c8c7314a79591b0f8b45cd/html5/thumbnails/63.jpg)
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
![Page 64: 2010-02-09 Reactor Pattern & Event Driven Programming](https://reader034.fdocuments.net/reader034/viewer/2022052618/54c8c7314a79591b0f8b45cd/html5/thumbnails/64.jpg)
how Thin works
Sorry! To be continued......
![Page 65: 2010-02-09 Reactor Pattern & Event Driven Programming](https://reader034.fdocuments.net/reader034/viewer/2022052618/54c8c7314a79591b0f8b45cd/html5/thumbnails/65.jpg)
how Thin works
Sorry! To be continued......
?