Your SlideShare is downloading. ×
  • Like
Don’t block the event loop!
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Now you can save presentations on your phone or tablet

Available for both IPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Don’t block the event loop!

  • 1,956 views
Published

Don’t block the event loop! …

Don’t block the event loop!

EventMachine: Scalable non-blocking i/o in ruby

Published in Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
1,956
On SlideShare
0
From Embeds
0
Number of Embeds
1

Actions

Shares
Downloads
23
Comments
0
Likes
8

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
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n

Transcript

  • 1. Don’t block the event loop! EventMachine: Scalable non-blocking i/o in ruby
  • 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. Blocking IO sucks
  • 4. Evented IO rules
  • 5. CXXXK problem
  • 6. CXXXK problemRPS(QPS/TPS)TPRConcurrency Level
  • 7. Efficient Processing Focus on maximizing machine utilization Google tries for ~80% utilization
  • 8. Different non blocking i/o asynchronous programming
  • 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. Reactor PatternWhat’s reactor?What’s design patterns?So, what’s reactor pattern?
  • 11. reactor reactor is simply a single thread, endless loop It reacts to incoming events
  • 12. design patternshttp://en.wikipedia.org/wiki/Pattern
  • 13. reactor pattern resources (description/file handler) synchronous event demultiplexer (select/epoll/kqueue) dispatcher request handler
  • 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. Let’s play with EM gem install eventmachine --pre
  • 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. EM.next_tickqueue a proc to be executed on the next iteration ofthe reactor loop
  • 18. EM.tick_loop 80k+/s on my mac air
  • 19. EM::Iterator That’s cool for writing crawler
  • 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. EM::Timer
  • 22. EM::Queueasync queueif queue is empty, it is invoked when someone calls#push
  • 23. EM::Channel
  • 24. EM.system
  • 25. Network Servers and ClientsEM.start_server / EM.stop_serverEM.connect / EM.bind_connectEM.open_datagram_socket
  • 26. TCP is stream
  • 27. And moreread eventmachine source code!
  • 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. Fiber What’s fiber? How to use fiber?
  • 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. use fiber http://ruby-doc.org/core-1.9.3/Fiber.html
  • 32. Solotion to callback hell Fiber into EM = em-synchrony thanks to Ilya Grigorik(CTO of PostRank)
  • 33. The End