Hi everyone,
First of all, EventMachine is a very cool project. I think it will be
important to building high-performance event driven apps in the
future.
I''ve created a few classes that add simple pipelining using
continuations and EventMachine. It makes it easy for you to convert
non-event driven code into event-driven code. I''m using it to
accelerate Merb, but I thought others might be interested in the
pattern. Here is an example:
Let''s say I have the following method, which can take long enough to
execute in response to an incoming HTTP request to block other events
(This is obviously contrived, but I hope it communicate the point):
## Entry point called by some low-level event handler from
EventMachine.
def handle_request(request)
headers, data = self.parse_request(request)
response = handler(headers, data)
self.send_response(response)
end
## This method renders the hash into XML. It can take while to run as
well.
def render_response(hash)
hash.to_xml
end
## And here is my handler. This contains application code written by
someone else.
def handler(headers, data)
hash = get_data_for_request(headers)
return self.render_response(hash)
end
---
As a framework guy, the handler is usually written by someone else, so
I can''t rewrite all of this code; just the support functions. So how
do I pipeline it? Like this:
def handle_request(request)
headers, data = self.parse_request(request)
pipeline { response = handler(headers, data) }
self.send_response(response)
end
## This method renders the hash into XML. It can take while to run as
well.
def render_response(hash)
ret = nil
pipeline { ret = hash.to_xml }
return ret
end
----
Now requests will be handled in five events:
Event 1: Run handle_request(), schedule event to run handler().
Event 2: Run handler() which calls render_response(). render_response
schedules event to generate XML.
Event 3: Generate XML, schedule event to resume render_response()
Event 4: Resume render_response() after pipeline. Return rendered
XML. Schedule event to resume handle_request().
Event 5: Resume handle_request(), send rendered response to server.
---
The pipeline system is built on continuations (which are expensive)
and Deferrables so it is not something you should use all the time,
but it is a great way to convert code that is not necessarily event
driven into something more compatible with the event driven model.
You can blend it with other more explicitly event-driven code to build
a fairly fast interleaved system without requiring all of your code to
go event-driven or depending on background threads (which is still
sometimes appropriate).
---
Now this code is really simple. It''s only one class and a couple of
helper methods. I would much rather just contribute it to the
EventMachine project than try to host it myself, but it looks like the
project is not taking new patches regularly. What does everyone here
recommend I do with this code? Submit it somewhere? Host it under my
own project?
Thanks for your help!
-Charles
http://www.okito.net