Your SlideShare is downloading. ×
0
Don’t block the event loop!    EventMachine: Scalable non-blocking i/o in ruby
Jinpu Hu (胡金埔)Blog: http://hujinpu.com/Github: https://github.com/hujinpuTwitter: https://twitter.com/#!/hujinpuSina weibo...
Blocking IO sucks
Evented IO rules
CXXXK problem
CXXXK problemRPS(QPS/TPS)TPRConcurrency Level
Efficient Processing Focus on maximizing machine utilization Google tries for ~80% utilization
Different non blocking i/o asynchronous programming
• Blocking IO a UNIVERSAL problem• All programming languages have trouble with it• The Reactor pattern resolves it• Most l...
Reactor PatternWhat’s reactor?What’s design patterns?So, what’s reactor pattern?
reactor reactor is simply a single thread, endless loop It reacts to incoming events
design patternshttp://en.wikipedia.org/wiki/Pattern
reactor pattern resources (description/file handler) synchronous event demultiplexer (select/epoll/kqueue) dispatcher reque...
What’s eventmachine?Ruby implementation of the Reactor patternSingle threaded by defaultAllows us to interleave multiple I...
Let’s play with EM gem install eventmachine --pre
EM.runtakes a block to execute once the reactor has startedEM.run is blockingthe loop takes over the ruby process, so code...
EM.next_tickqueue a proc to be executed on the next iteration ofthe reactor loop
EM.tick_loop                  80k+/s               on my mac air
EM::Iterator That’s cool for writing crawler
EM.deferon first invocation, spawns up EM.threadpool_sizethreads in a poolall the threads read procs from a queue and execu...
EM::Timer
EM::Queueasync queueif queue is empty, it is invoked when someone calls#push
EM::Channel
EM.system
Network Servers and ClientsEM.start_server / EM.stop_serverEM.connect / EM.bind_connectEM.open_datagram_socket
TCP is stream
And moreread eventmachine source code!
Callback Hellvar   useFile = function(filename,callback){      posix.stat(filename).addCallback(function (stats) {        ...
Fiber What’s fiber? How to use fiber?
what’s fiber light weight cooperative concurrency the scheduling must be done by the programmer and not the VM a small 4KB ...
use fiber http://ruby-doc.org/core-1.9.3/Fiber.html
Solotion to callback hell Fiber into EM = em-synchrony thanks to Ilya Grigorik(CTO of PostRank)
The End
Don’t block the event loop!
Don’t block the event loop!
Upcoming SlideShare
Loading in...5
×

Don’t block the event loop!

2,054

Published on

Don’t block the event loop!

EventMachine: Scalable non-blocking i/o in ruby

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

No Downloads
Views
Total Views
2,054
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
24
Comments
0
Likes
9
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Transcript of "Don’t block the event loop!"

    1. 1. Don’t block the event loop! EventMachine: Scalable non-blocking i/o in ruby
    2. 2. Jinpu Hu (胡金埔)Blog: http://hujinpu.com/Github: https://github.com/hujinpuTwitter: https://twitter.com/#!/hujinpuSina weibo: http://weibo.com/hujinpuEmail: hujinpu@gmail.com
    3. 3. Blocking IO sucks
    4. 4. Evented IO rules
    5. 5. CXXXK problem
    6. 6. CXXXK problemRPS(QPS/TPS)TPRConcurrency Level
    7. 7. Efficient Processing Focus on maximizing machine utilization Google tries for ~80% utilization
    8. 8. Different non blocking i/o asynchronous programming
    9. 9. • Blocking IO a UNIVERSAL problem• All programming languages have trouble with it• The Reactor pattern resolves it• Most languages have the Reactor pattern! JavaScript Node.js Python Twisted Ruby EventMachine Java MINA Lua LuaNode
    10. 10. Reactor PatternWhat’s reactor?What’s design patterns?So, what’s reactor pattern?
    11. 11. reactor reactor is simply a single thread, endless loop It reacts to incoming events
    12. 12. design patternshttp://en.wikipedia.org/wiki/Pattern
    13. 13. reactor pattern resources (description/file handler) synchronous event demultiplexer (select/epoll/kqueue) dispatcher request handler
    14. 14. What’s eventmachine?Ruby implementation of the Reactor patternSingle threaded by defaultAllows us to interleave multiple IO ops and a singleCPU op simultaneously
    15. 15. Let’s play with EM gem install eventmachine --pre
    16. 16. EM.runtakes a block to execute once the reactor has startedEM.run is blockingthe loop takes over the ruby process, so code afterEM.run will not runall your code(relative to em) must in EM.run
    17. 17. EM.next_tickqueue a proc to be executed on the next iteration ofthe reactor loop
    18. 18. EM.tick_loop 80k+/s on my mac air
    19. 19. EM::Iterator That’s cool for writing crawler
    20. 20. EM.deferon first invocation, spawns up EM.threadpool_sizethreads in a poolall the threads read procs from a queue and executethem in parallel to the reactor threadoptional second proc is invoked with the result of thefirst proc, but back inside the reactor thread
    21. 21. EM::Timer
    22. 22. EM::Queueasync queueif queue is empty, it is invoked when someone calls#push
    23. 23. EM::Channel
    24. 24. EM.system
    25. 25. Network Servers and ClientsEM.start_server / EM.stop_serverEM.connect / EM.bind_connectEM.open_datagram_socket
    26. 26. TCP is stream
    27. 27. And moreread eventmachine source code!
    28. 28. Callback Hellvar useFile = function(filename,callback){    posix.stat(filename).addCallback(function (stats) {        posix.open(filename, process.O_RDONLY, 0666).addCallback(function (fd) {            posix.read(fd, stats.size, 0).addCallback(function(contents){                callback(contents);            });        });    });};
    29. 29. Fiber What’s fiber? How to use fiber?
    30. 30. what’s fiber light weight cooperative concurrency the scheduling must be done by the programmer and not the VM a small 4KB stack
    31. 31. use fiber http://ruby-doc.org/core-1.9.3/Fiber.html
    32. 32. Solotion to callback hell Fiber into EM = em-synchrony thanks to Ilya Grigorik(CTO of PostRank)
    33. 33. The End
    1. A particular slide catching your eye?

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

    ×