Raising Events Asynchronously

This post is by Phil Windley's Technometria from Phil Windley's Technometria

Click here to view on the original site: Original Post

I've often described Sky, the Kynetx event protocol, as an "event-response" protocol, a cross between a synchronous request-response protocol and an asynchronous event protocol. Sky has always processed events synchronously, even when no response was necessary. There are many times that the event generator does not need a response from the processor and would just as soon get on with its business. I imagined all kinds of complicated ways to make Sky asynchronous.

Mark Horstmeier had an ingenious idea: just use the mod_perl cleanup handler. Since we're using Apache as our application server this made things relatively easy. The cleanup handler runs as the very last thing in the HTTP request cycle. Consequently, the response has already been sent back by the time it runs as this document from the mod_perl project shows:

Mark simply moved the synchronous handler to the name _handler and then registers it with the clean-up handler if the _async parameter is true, or calls _handler directly if it's not:

sub handler {
  my $r = shift;	
  my $req = Apache2::Request->new($r);
  if ($req->param('_async'))	{
    //------ Asynchronous evaluation---------
    return Apache2::Const::OK;
  } else {
    //------ Synchronous evaluation----------
    return Apache2::Const::OK;

A few lines of carefully crafted Perl and we have asynchronous events. Beautiful!

The first use of this is to make the event:send() action asynchronous by default. This action is most often used in situations where events are being sent to a number of other personal event networks. While event:send() is engineered to send those events in parallel, you still have to wait for them to return and the overall execution time is at least as long as the longest. With asynchronous execution, this is no longer the case.

Tags: krl kre events apache mod_perl

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.