EVENTMACHINE  Defeating blocking I/O
BLOCKING I/O                   synchronous                    Start                    Wait                    Result     ...
NON-BLOCKING I/O            asynchronous    Initiate I/O    Do something           Kernelt   I/O Callback
WHEN IS BLOCKING I/O BADMost applications are I/O Bound         Higher CPU utilization         Higher throughput / process...
STRATEGIES FOR CONCURRENCYProcessThreads          Threadpool          Thread/ requestReactor Pattern
REACTOR PATTERN•   Single thread•   Event sources (files, sockets, keyboards, timers)•   Schedule synchronous-code blocks o...
EVENTMACHINEReactor pattern in practice + threadpool
EVENTMACHINE• Event-driven• Threadpool   for blocking/legacy code• Protocol   implementations                   Event call...
EVENTMACHINE BASICSputs "Start"               >   StartEM.run do                  >   Init  puts "Init"              >   O...
EM.REACTOR_RUNNING?puts EM.reactor_running?                                 >   falseEM.run do                            ...
EM.DEFER            Put it on the threadpoolp Thread.current                 #<Thread:0x00000100887678>                   ...
EM.ADD_PERIODIC_TIMERputs EM.reactor_running?        > TickEM.run do                       > Tick  EM.add_periodic_timer(1...
EM::DEFERRABLE        Manage callback spaghetti like a bossclass Ping  include EM::Deferrable  def run      puts “Ping”   ...
EM::DEFERRABLE        Manage callback spaghetti like a bossEM.run do                             > Ping  png = Ping.new   ...
EM::QUEUE             An in-memory queueq = EM::Queue.new                 > OneEM.run do                         > Two  q....
SERVERS USING                    EVENTMACHINE• Thin• Rainbow• Practical   use  • async_sinatra  • Roll   your own server w...
A WORD OF CAUTION• Don’t   use it when you don’t need it• Difficult   to debug  • EM.defer   suppresses exceptions• Favor  ...
QUESTIONS@noverloophttp://noverloop.benicolas@noverloop.be
Upcoming SlideShare
Loading in …5
×

Defeating blocking I/O with eventmachine

6,815 views

Published on

A short introduction talk into eventmachine

Published in: Technology, Business
  • Be the first to comment

Defeating blocking I/O with eventmachine

  1. 1. EVENTMACHINE Defeating blocking I/O
  2. 2. BLOCKING I/O synchronous Start Wait Result tCommon operations File I/O Network I/O 13 ms 200ms - 2 seconds
  3. 3. NON-BLOCKING I/O asynchronous Initiate I/O Do something Kernelt I/O Callback
  4. 4. WHEN IS BLOCKING I/O BADMost applications are I/O Bound Higher CPU utilization Higher throughput / process Less Servers CostLoad times may benefit
  5. 5. STRATEGIES FOR CONCURRENCYProcessThreads Threadpool Thread/ requestReactor Pattern
  6. 6. REACTOR PATTERN• Single thread• Event sources (files, sockets, keyboards, timers)• Schedule synchronous-code blocks on triggers• Blocking I/O not allowed! • sleep(20) • do_really_long_computation • sleep 1 until some_condition == true • `pwd`
  7. 7. EVENTMACHINEReactor pattern in practice + threadpool
  8. 8. EVENTMACHINE• Event-driven• Threadpool for blocking/legacy code• Protocol implementations Event callbacks Spaghetti code
  9. 9. EVENTMACHINE BASICSputs "Start" > StartEM.run do > Init puts "Init" > Ok EM.add_timer(1) do # 1 sec pause puts “Quit” > Quit EM.stop_event_loop > Done end puts “Ok”endputs “Done”
  10. 10. EM.REACTOR_RUNNING?puts EM.reactor_running? > falseEM.run do > Waiting EM.add_timer(1) do # 1 sec pause puts EM.reactor_running? > true EM.stop > false end puts “Waiting”endputs EM.reactor_running?
  11. 11. EM.DEFER Put it on the threadpoolp Thread.current #<Thread:0x00000100887678> #<Thread:0x00000100887678>EM.run do #<Thread:0x0000010215e458> #<Thread:0x00000100887678> p Thread.current EM.defer do p Thread.current EM.next_tick do p Thread.current EM.stop end endend
  12. 12. EM.ADD_PERIODIC_TIMERputs EM.reactor_running? > TickEM.run do > Tick EM.add_periodic_timer(1) do > Stop puts “Tick” end EM.add_timer(3) do puts “Stop” EM.stop endend
  13. 13. EM::DEFERRABLE Manage callback spaghetti like a bossclass Ping include EM::Deferrable def run puts “Ping” EM.add_timer(3) do # add evented code succeed "Pong" end endend
  14. 14. EM::DEFERRABLE Manage callback spaghetti like a bossEM.run do > Ping png = Ping.new #3s wait png.callback do |res| > Pong puts res EM.stop end png.runend
  15. 15. EM::QUEUE An in-memory queueq = EM::Queue.new > OneEM.run do > Two q.push(‘one’,‘two’,‘three’) > Three 3.times do q.pop {|msg| puts msg } endend
  16. 16. SERVERS USING EVENTMACHINE• Thin• Rainbow• Practical use • async_sinatra • Roll your own server with eventmachine
  17. 17. A WORD OF CAUTION• Don’t use it when you don’t need it• Difficult to debug • EM.defer suppresses exceptions• Favor stability in production systems• Use epoll on linux and set `ulimit -n 32000`
  18. 18. QUESTIONS@noverloophttp://noverloop.benicolas@noverloop.be

×