On сре, 15 јан 2014 05:08:49 CET, Roberto De Ioris wrote:

Hi there, we have an API that requires consistent high-performance, and as
part of profiling we've identified that large writes to memcache cause
problems. We would like to write to memcache after we have transmitted our
response to the client. My understanding is that (per the WSGI spec) we
ought to be able to yield our response and have that returned immediately
to Nginx, then do our memcache write after data has been transmitted to
the
client.

Something like:
def handle_request():
   response = make_response()
   yield response # Response data goes out over the network
   memcache.write(response)


Hi, it is a perfectly valid usage

Another option would be to use the close() method supported by WSGI

»
If the iterable returned by the application has a close() method, the server or gateway must call that method upon completion of the current request, whether the request was completed normally, or terminated early due to an error. (This is to support resource release by the application. This protocol is intended to complement PEP 325's generator support, and other common iterables with close() methods.
«

Here's a simple example. Unfortunately uWSGI doesn't return the HTTP result (and close the http connection) until the sleep finishes, and my reading of the PEP should allow that.

# app.wsgi
# uwsgi --plugins python2,gevent -M --http-socket :8080 --wsgi-file app.wsgi --gevent 100
import gevent

class App(object):
   def __call__(self, env, start_response):
       start_response('200 OK', [('Content-Type','text/plain')])
       return self

   def __iter__(self):
       yield "hello world"

   def close(self):
       gevent.sleep(4)
       print "finished!"

application = App()



--
дамјан
_______________________________________________
uWSGI mailing list
[email protected]
http://lists.unbit.it/cgi-bin/mailman/listinfo/uwsgi

Reply via email to