EventMachine Examples

I needed to create a simple, but IO-intensive, thumbnailing service for OneSpot last week. This service acts as a proxy to S3 and so a blocking implementation would not scale well, even if threaded. I wanted to use EventMachine instead. Lessons learned:

  • The programming model is a mind twist and takes a long time to understand. The sprinkling of implementation at various layers makes it harder. I’ve spent several days now reading through EventMachine, Thin, Rack and em-http-request source code.
  • There’s no non-trivial examples out there. It seems like every example is 10 lines of “Hello World” code with no samples of how to integrate multiple pieces. Ok, here’s a 10 line async web server. Now how do I integrate an async call to the DB? How do I make an async 3rd party web service call?
  • There’s no testing support. No libraries for doing async testing and no best practices or suggestions on how to test.

So how can we make things better rather than just complain? I’m going to show you a non-trivial example. In return, I want you to send me more examples. Evented is my new Github repository for EventMachine examples. The first example is that big chunk of code that I puzzled over for the last few days which implements the thumbnailing service using Thin, S3, image_science and em-http-request. But I want more examples and I’d love to hear ideas on how to test this type of code. Leave a comment, send a pull request, and help me help you!

4 thoughts on “EventMachine Examples”

  1. I had a similarly challenging go with EventMachine at Scout Labs. It’s an amazing piece of work.

    The trick was understanding:

    - the main EventMachine.run block’s never-exit behavior; to see exceptions, you have to rescue, EventMachine.stop_event_loop, & raise

    - that the main run block must return so that events can be processed; I use ProcessChannel.subscribe & push to generate the async calls from the run block

    - how to act like a loop inside that main block, since it must return; at the end of each iteration I use EventMachine::Timer to schedule the next iteration

    - how to track & limit outstanding events so that my service doesn’t run out of control


  2. Having similar problems learning EventMachine.. All the tutorials/examples out there are irrelevant to objects/variables/databases beyond the loop construct. Worse yet there are many that setup echo server ‘examples’ without an example client with which to pass data… So far I’ve tried 5 different EM “server” examples that demonstrate no qualities beyond a simple ruby loop. As if that weren’t enough, one of the first examples google returns is a script containing characters that ruby cannot interpret. Finally, all of the connection-based examples on blocking loops is todo with (and requires) HTTP, whereas a simple ‘sleep 10′ or ’100000**100000′ would have sufficed.

    Chris is angry.

  3. I realize this post is quite old, but there still doesn’t seem to be much support for testing rack apps using EventMachine. I took a stab at extending rack-test to effectively turn an async rack app into a sync one, just for the sake of testing. It is my first stab at it, so I’d love to get some feedback: https://github.com/thoughtless/async_rack_test

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>