This document contains information about EventMachine and the reactor pattern in Ruby. It discusses how EventMachine implements the reactor pattern to allow non-blocking and asynchronous I/O in Ruby. It provides examples of how to use EventMachine to run code asynchronously using callbacks, timers, queues, and fibers to avoid "callback hell". The document also lists several other languages and frameworks that use the reactor pattern for 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
14. What’s eventmachine?
Ruby implementation of the Reactor pattern
Single threaded by default
Allows us to interleave multiple IO ops and a single
CPU op simultaneously
16. EM.run
takes a block to execute once the reactor has started
EM.run is blocking
the loop takes over the ruby process, so code after
EM.run will not run
all your code(relative to em) must in EM.run
20. EM.defer
on first invocation, spawns up EM.threadpool_size
threads in a pool
all the threads read procs from a queue and execute
them in parallel to the reactor thread
optional second proc is invoked with the result of the
first proc, but back inside the reactor thread