Skip to content
bwalton edited this page Sep 14, 2010 · 5 revisions

Notes about using EventMachine

BrB runs on the EventMachine reactor.

A reactor is a single thread that can handle hundreds of connections, so be careful to not lock it with long running tasks in your threads. In order to handle those longer running tasks, EventMachine uses spawned, independant threads called workers. If you know you’re going to have such tasks, place them in a defer block like so:

EM.defer do
  # Long running tasks here
end

How does BrB handle this?

BrB runs requests of your object through EM.defer, but due to the limitations of worker threads (described above) BrB will have to wait for a slot to open up to continue processing your other threads. This can be a major issue if you’re doing things that require something like a real-time server.

What are our thread options?

In Ruby 1.8, Threads are implemented with green threads (a virtual thread running in the Ruby VM), that means that if you do a system call in any of your threads, all your processes will be frozen waiting for the response. Threads will not be able to execute until this call is complete.

For example, if you do a long request using the MySQL gem, the EM reactor will be frozen until your call is complete.

But, JRuby can help us with this. JRuby runs on real system threads, so your code is not blocked. BrB works well on JRuby and I really encourage you to give it a try for best performance.