• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Don’t block the event loop!

Don’t block the event loop!



Don’t block the event loop!

Don’t block the event loop!

EventMachine: Scalable non-blocking i/o in ruby



Total Views
Views on SlideShare
Embed Views



7 Embeds 248

http://hujinpu.com 232
http://hujinpu.github.com.dev 8
http://feeds.feedburner.com 3
http://www.hanrss.com 2
https://twimg0-a.akamaihd.net 1
https://si0.twimg.com 1
http://as3.brite.biz 1



Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment
  • \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

Don’t block the event loop! Don’t block the event loop! Presentation Transcript

  • 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: http://weibo.com/hujinpuEmail: hujinpu@gmail.com
  • 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 languages have the Reactor pattern! JavaScript Node.js Python Twisted Ruby EventMachine Java MINA Lua LuaNode
  • 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 request handler
  • What’s eventmachine?Ruby implementation of the Reactor patternSingle threaded by defaultAllows us to interleave multiple IO ops and a singleCPU op simultaneously
  • 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 afterEM.run will not runall your code(relative to em) must in EM.run
  • 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 executethem in parallel to the reactor threadoptional second proc is invoked with the result of thefirst proc, but back inside the reactor thread
  • 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) {        posix.open(filename, process.O_RDONLY, 0666).addCallback(function (fd) {            posix.read(fd, stats.size, 0).addCallback(function(contents){                callback(contents);            });        });    });};
  • 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 stack
  • 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