# Python wsgi behind nginx: yield not behaving as expected

**URL:** <https://www.ruby-forum.com/t/python-wsgi-behind-nginx-yield-not-behaving-as-expected/213477>\
**Category:** NGINX\
**Created:** [November 3, 2011, 8:09am UTC](https://www.ruby-forum.com/t/python-wsgi-behind-nginx-yield-not-behaving-as-expected/213477 "2011-11-03T08:09:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [November 3, 2011, 8:09am UTC](https://www.ruby-forum.com/t/python-wsgi-behind-nginx-yield-not-behaving-as-expected/213477/1 "2011-11-03T08:09:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Matt\_Starcrest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/dbc845/32.png) [@Matt\_Starcrest](https://www.ruby-forum.com/u/Matt_Starcrest)\
**Post date:** [November 3, 2011, 8:13am UTC](https://www.ruby-forum.com/t/python-wsgi-behind-nginx-yield-not-behaving-as-expected/213477/2 "2011-11-03T08:13:13Z")

</div>

On Thu, Nov 03, 2011 at 12:08:49AM -0700, Matt Starcrest wrote:

> 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?

In nginx-1.0.9 or nginx-1.1.5 you can try  
uwsgi\_buffering off;  
or  
sgi\_buffering off;  
depending on protocol. In any modern enough nginx version you can use  
proxy\_buffering off;

There is no way currently to disable buffering of FastCGI servers.

–  
Igor S.

---

<div class="post-metadata">

**Author:** ![Matt\_Starcrest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/dbc845/32.png) [@Matt\_Starcrest](https://www.ruby-forum.com/u/Matt_Starcrest)\
**Post date:** [November 3, 2011, 8:49am UTC](https://www.ruby-forum.com/t/python-wsgi-behind-nginx-yield-not-behaving-as-expected/213477/3 "2011-11-03T08:49:01Z")

</div>

Looks like that solved it! Thanks Igor!

* * *

From: Igor S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
To: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Sent: Thursday, November 3, 2011 12:12 AM  
Subject: Re: python wsgi behind nginx: yield not behaving as expected

On Thu, Nov 03, 2011 at 12:08:49AM -0700, Matt Starcrest wrote:

> 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?

In nginx-1.0.9 or nginx-1.1.5 you can try  
uwsgi\_buffering off;  
or  
sgi\_buffering off;  
depending on protocol. In any modern enough nginx version you can use  
proxy\_buffering off;

There is no way currently to disable buffering of FastCGI servers.

–  
Igor S.

---

<div class="post-metadata">

**Author:** ![Matt\_Starcrest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/dbc845/32.png) [@Matt\_Starcrest](https://www.ruby-forum.com/u/Matt_Starcrest)\
**Post date:** [November 3, 2011, 8:52am UTC](https://www.ruby-forum.com/t/python-wsgi-behind-nginx-yield-not-behaving-as-expected/213477/4 "2011-11-03T08:52:06Z")

</div>

On Thu, Nov 03, 2011 at 12:48:30AM -0700, Matt Starcrest wrote:

> Looks like that solved it! Thanks Igor!

Please note, that any “…\_buffering off” disables caching,  
so you can set it exactly on locations which should be cached:

location /some/page {  
…\_buffering off;  
}

> > output = get\_response\_quickly(environ)  
> > In nginx-1.0.9 or nginx-1.1.5 you can try  
> > Igor S.
> 
> * * *
> 
> nginx mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [mailman.nginx.org archive](http://mailman.nginx.org/mailman/listinfo/nginx)

> * * *
> 
> nginx mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [mailman.nginx.org archive](http://mailman.nginx.org/mailman/listinfo/nginx)

–  
Igor S.
