<html><body><div style="color:#000; background-color:#fff; font-family:times new roman, new york, times, serif;font-size:12pt"><div style="font-family: 'times new roman', 'new york', times, serif; font-size: 12pt; ">Hi,</div><div style="font-family: 'times new roman', 'new york', times, serif; font-size: 12pt; ">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:</div><div style="font-family: 'times new roman', 'new york', times, serif; font-size: 12pt; "><br></div><div><div><font class="Apple-style-span" face="'times new roman', 'new york', times, serif">def application(environ, start_response):</font></div><div><span class="Apple-style-span" style="font-family: 'times new roman', 'new york', times, serif; "> output =
<i>get_response_quickly</i>(environ)</span><br></div><div><font class="Apple-style-span" face="'times new roman', 'new york', times, serif"> start_response('200 OK', </font><span class="Apple-style-span" style="font-family: 'times new roman', 'new york', times, serif; ">[('Content-type', 'text/plain'), </span><span class="Apple-style-span" style="font-family: 'times new roman', 'new york', times, serif; ">('Content-Length', str(len(output)))]</span><font class="Apple-style-span" face="'times new roman', 'new york', times, serif">)</font><br></div><div><font class="Apple-style-span" face="'times new roman', 'new york', times, serif"> yield output</font></div><div><font class="Apple-style-span" face="'times new roman', 'new york', times, serif"> <i>do_slow_work</i>()</font></div><div><font class="Apple-style-span" face="'times new roman', 'new york', times, serif"><br></font></div><div style="font-family:
'times new roman', 'new york', times, serif; font-size: 12pt; ">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.</div><div style="font-family: 'times new roman', 'new york', times, serif; font-size: 12pt; "><br></div><div style="font-family: 'times new roman', 'new york', times, serif; font-size: 12pt; ">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?</div><div style="font-family: 'times new roman', 'new york', times, serif; font-size: 12pt; "><br></div><div style="font-family: 'times new roman', 'new york', times, serif;
font-size: 12pt; ">Thanks,</div><div style="font-family: 'times new roman', 'new york', times, serif; font-size: 12pt; ">Matt</div></div></div></body></html>