"Hacking" the event model of Nginx

Manlio Perillo manlio_perillo at libero.it
Sat Apr 12 15:01:16 MSD 2008


François Battail ha scritto:
> Hi All!
> 
> I'm new on this nice discussion list but I'm a long time lurker.
> 
> I'm working on a very specific module for Nginx: a complete CMS. It may
> sounds strange since upstream servers and scripting languages are the
> norm for the CMS, but I want (and need) speed.
> 

If you need "speed", try my WSGI module.
http://hg.mperillo.ath.cx/nginx/mod_wsgi/

It enables you to write Python applications embedded in Nginx.

Of course there are some overheads, but usually the problems are elsewhere.

http://shootout.alioth.debian.org/gp4/benchmark.php?test=all&lang=all

Python is not really that bad, unless, of course, you start to use a lot 
of for cycles or recursion (but in this case you can always write the 
function in C).


> So far I've done many things without too much trouble, but I'm a little
> bit stuck with the processing of events in Nginx. I would like to
> process a specific event which is not connection related but created by
> one of my worker threads.
> 

Give at look at:
http://hg.mperillo.ath.cx/nginx/mod_wsgi/file/tip/src/ngx_wsgi.c
row 850.

You need to obtain a valid file descriptor `s`, then call:
c = ngx_get_connection(s, log);

The event for read and write notifications are:
c->read and c->write

Note however that in my code, the file descriptor is assumed as already 
"connected" to the peer.


> I hope this short example will be clear:
> 
> // My specific Nginx http handler
> int my_http_handler (ngx_request_t * r)
> {
>   if (r->my_state == 0) // first step: initiate the work to do
>   {
>     r->my_state ++ ;
>     my_sendmsg (myqueue,r) ; // send a message to worker
> 
>     return NGX_AGAIN ; // please call me back when done
>   }
>   else // second step: results are ready
>   {
>     // produce xhtml output from results
>     return NGX_OK ; // finished
>   }
> }
> 
> // The worker running on a specific thread
> void my_http_worker (void * arg)
> {
>   ngx_http_request_t * r ;
>   ngx_event_t * ev ;
> 
>   while (1)
>   {
>     my_recvmsg (myqueue,r) ;
>     // processing the request
>     // ...
>     // wake up my_http_handler
>     ngx_post_event (ev, (ngx_event_t * *) & ngx_posted_events) ;
>   }
> }
> 
> But I don't know how to fill the ngx_event_t (in particular the
> handlers) in order to call again my_http_handler on Nginx's context.
> I believe it's possible to do so from what I've seen, but Nginx's code
> is not so easy to enter on (it's not a critic).
> 

Is my_http_worker in a separate thread?
Then this will not work, Nginx is not thread safe.

> Sorry for this (first) long message but I think it would be nice to be
> able to develop clean and non blocking modules for Nginx.
> 
> BTW forgive my English, I'm French ;-)
> 
> 


Regards  Manlio Perillo





More information about the nginx mailing list