Event-driven handler

Manlio Perillo manlio_perillo at libero.it
Wed Feb 18 19:00:18 MSK 2009


Marcus Clyne ha scritto:
> Hi,
> 
> I'm trying to develop a handler for Nginx.  I want to do this:
> 
> (1) Request comes in to Nginx
> (2) This is passed on to handler
> (3) Handler sends request to other code (either a new thread or some
> event-driven system) 

Be aware that Nginx does not support threads, and it is not thread safe.
Of course you can create and manage your threads, but that's not easy.

What do you mean by "some event-driven system"?

> and tells Nginx to wait until output has been generated

This is easy.
You just have to return NGX_DONE from the handler, and call 
ngx_http_finalize_request when done.

You may be interested in my mod_wsgi code (ngx_http_wsgix_handler.c) 
code for more details
http://hg.mperillo.ath.cx/nginx/mod_wsgi/file/tip/src/ngx_http_wsgi_handler.c

> (4) Output is generated in 'background' whilst Nginx is handling other requests

This, again, *may* be easy (if you don't use threads, of course).

> (5) Message is sent to Nginx that content has been generated through some event
> (e.g. with a semaphore)

No need for this, if you avoid threads.
And if you use threads, this is not that simple.

> (6) Nginx retrieves content and sends to client
> 
> I already have a handler that does 1,2, 4 (not in background though) and 6 -
> it's the 3, 4 (in background) and 5 that I don't know how to do yet. My current
> implementation is a blocking one, which I don't want.
> 
> Could anyone please give me a simple example of how to do this, or at least tell
> me which functions I need to use?
> 

If you can refactor you code, I suggest to execute the computation by steps.

Set a timer in Nginx, say to 0.001 seconds, so that at each event loop 
cycle your code is executed.

The code should execute some piece of computation quickly (storing state 
in an Nginx request context), and return.

Of course if the codes involves some IO, this may be tricky.
But you can use the Nginx event module to receive IO notifications.


> Thanks.
> 


Regards  Manlio Perillo





More information about the nginx mailing list