Event-driven handler

Manlio Perillo manlio_perillo at libero.it
Thu Feb 19 14:30:27 MSK 2009

Marcus Clyne ha scritto:
>  > What do you need to do?
> I'm generating pages based on simple templates, but which could do all 
> manner of things, including getting data from databases. I don't want to 
> use PHP/Python etc because the overhead would be very high and not very 
> practical for what I'm doing.

This is an incorrect generalization!

If your content generation is computationally expensive (both CPU and IO 
bound), then the overhead of the Python interpreter is usually much 
smaller compared to the overhead of your computation; and you can always 
code your computationally expensive routine as a C extension.

However, for what you want to do, Nginx is the wrong choice.
You should use something else like:

1) CGI, if your computation is CPU bound, and you don't need a 
persistent connection to the database
2) Apache, if you want flexibility
3) Erlang, if you want a complete environment for writing both CPU and 
IO bound scalable web applications

> [...]
> You mentioned setting up a loop that checked every 0.001 secs.  How 
> about a solution like this:
> [handler] (as mentioned before)
> - passes off request to content generator
> - returns immediately to Nginx with NGX_DONE
> [content generator]
> - generates content
> - in threadsafe fashion, puts the output content in a queue of responses 
> to be returned
> [in Nginx]
> - loop that checks every e.g. 0.001 secs to see if there are any 
> responses (headers and/or content)
> - checks for responses in threadsafe fashion, and if there are, calls 
> ngx_http_output_filter etc

This is called polling, and is generally the wrong solution.

> [...]

> Alternatively, I would prefer to have an event-driven system whereby I 
> wouldn't need a loop to check whether
> my responses were ready.  

Note that you can't have a second "loop" in Nginx (unless this is in a 
separate thread).

> Is it possible to trigger non-IO events in 
> Nginx, such that once triggered, Nginx
> would handle them properly (even if they were called from within a 
> different thread)?

The Nginx event loop is event based.
It uses epoll or kqueue.

With kqueue or epoll it is possible to receive notification for non IO 
events, like signals or filesystem events.

But in your case, you can use a simple pipe.
In the mailing list archive you can find an old discussione between me 
and Igor, about this topic.

 > [...]


More information about the nginx mailing list