Your SlideShare is downloading. ×
Defeating blocking I/O with eventmachine
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

Defeating blocking I/O with eventmachine

4,893
views

Published on

A short introduction talk into eventmachine

A short introduction talk into eventmachine

Published in: Technology, Business

0 Comments
11 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
4,893
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
0
Comments
0
Likes
11
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
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Transcript

    • 1. EVENTMACHINE Defeating blocking I/O
    • 2. BLOCKING I/O synchronous Start Wait Result tCommon operations File I/O Network I/O 13 ms 200ms - 2 seconds
    • 3. NON-BLOCKING I/O asynchronous Initiate I/O Do something Kernelt I/O Callback
    • 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. STRATEGIES FOR CONCURRENCYProcessThreads Threadpool Thread/ requestReactor Pattern
    • 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. EVENTMACHINEReactor pattern in practice + threadpool
    • 8. EVENTMACHINE• Event-driven• Threadpool for blocking/legacy code• Protocol implementations Event callbacks Spaghetti code
    • 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. 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. 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. 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. 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. 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. 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. SERVERS USING EVENTMACHINE• Thin• Rainbow• Practical use • async_sinatra • Roll your own server with eventmachine
    • 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. QUESTIONS@noverloophttp://noverloop.benicolas@noverloop.be