python wsgi behind nginx: yield not behaving as expected

Matt Starcrest matt.starcrest at yahoo.com
Thu Nov 3 07:08:49 UTC 2011


Hi,
I want to run wsgi python code on a web server behind nginx.  The server needs to respond quickly to an http request, then continue to do some (slow) work after responding.  Python's yield statement seems to fit the bill, as follows:

def application(environ, start_response):
    output = get_response_quickly(environ)

    start_response('200 OK', [('Content-type', 'text/plain'), ('Content-Length', str(len(output)))])

    yield output
    do_slow_work()

If I run this wsgi in a standalone python server (I tried wsgiref, fapws, uwsgi), the caller receives the output immediately (after get_response_quickly()), as desired.  Great.  However, if I run any of these servers behind nginx, the caller doesn't receive a response until *after* do_slow_work() -- thus defeating the purpose.

Is there a way to make this pattern work with nginx?  Or is there a better way in general to respond quickly but continue work, without manually creating python threads / other clumsiness?

Thanks,
Matt
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.nginx.org/pipermail/nginx/attachments/20111103/80b81e19/attachment.html>


More information about the nginx mailing list