Event-driven handler

Eugaia eugaia at gmail.com
Wed Feb 18 20:25:09 MSK 2009

> 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.
Thanks, yes I am aware - and I'll be handling the thread stuff myself.
> What do you mean by "some event-driven system"?
I'm talking about how I'll generate my content here - it's irrelevant to 
my question about Nginx.
>> 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.
This may be what I need to know.  If I return NGX_DONE, is the request 
the response then not automatically
sent to the client?  In order to do that, you need to call 
ngx_http_finalize_request. Is that right?

What about if I want to use filters after my content has been 
generated?  E.g. gzip.  At the moment, my
code calls ngx_http_output_filter in my handler and returning the 
response from that.

Should I instead do something like:

[my handler]
- sends request to my output-generating code (which may be in its own 
new thread)
- immediately returns NGX_DONE (which then sends no response to the client)

[my output-generating code]
- generates output (e.g. in its own thread)
- returns ngx_http_output_filter(...) (if wanting to continue using filters)
- returns ngx_http_finalize_request(...) (if don't want to use any filters)

Does this sound right, or have I mis-understood what you wrote?
> 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 
Thanks, I'll check it out.
>> (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)
If I've understood your comments above, then this notification actually 
isn't necessary -
you just call ngx_http_finalize_request (or ngx_http_output_filter?) 
when you're done.

I was thinking that a response was automatically sent when you returned 
from your handler.
> If you can refactor you code, I suggest to execute the computation by 
> steps.
That's not really practical for what I'm doing - there's no guarantee 
that the code can be executed quickly.
> Of course if the codes involves some IO, this may be tricky.
> But you can use the Nginx event module to receive IO notifications.
Could you possibly give me an example?



More information about the nginx mailing list