Want more features on Pastebin? Sign Up, it's FREE!
Guest

Manlio Perillo

By: a guest on Mar 30th, 2010  |  syntax: Python  |  size: 2.28 KB  |  views: 93  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. """Simple test suite for ngx_http_wsgi_module.
  2.  
  3. THIS SOFTWARE IS UNDER MIT LICENSE.
  4. Copyright (c) 2010 Perillo Manlio (manlio.perillo@gmail.com)
  5.  
  6. Read LICENSE file for more informations.
  7. """
  8.  
  9. from greenlet import getcurrent, greenlet
  10.  
  11.  
  12. class middleware(object):
  13.     """A middleware that use greenlet to provide a write callable with
  14.    the correct code flow.
  15.    """
  16.  
  17.     def __init__(self, application):
  18.         self.app = application
  19.  
  20.     def __call__(self, environ, start_response):
  21.         def _start_response(status, headers):
  22.             start_response(status, headers)
  23.             return self.write
  24.  
  25.         # Initialize greenlet and state
  26.         gp = greenlet(self.app)
  27.         self.write_allowed = True
  28.  
  29.         r = gp.switch(environ, _start_response)
  30.         while isinstance(r, str):
  31.             # Got data from the write callable
  32.             yield r
  33.  
  34.             r = gp.switch()
  35.  
  36.         # The application returned the app_iter object
  37.         # From now on, it can no more call the write callable
  38.         # XXX check me
  39.         self.write_allowed = False
  40.         for buf in r:
  41.             yield buf
  42.  
  43.     def write(self, buf):
  44.         gp = getcurrent()
  45.  
  46.         if self.write_allowed:
  47.             gp.parent.switch(buf)
  48.         else:
  49.             exc = ValueError(
  50.                 'write callable called from application iterator')
  51.             gp.throw(exc)
  52.  
  53.  
  54. BUFSIZE = 128
  55. N = 50
  56.  
  57. @middleware
  58. def application(environ, start_response):
  59.     """A simple WSGI application that use the write callable.
  60.    """
  61.  
  62.     headers = [('Content-Type', 'text/plain')]
  63.  
  64.     write = start_response('200 OK', headers)
  65.     for i in range(0, N):
  66.         write('%02d' % i * BUFSIZE + '\n')
  67.  
  68.     return ['Test write greenlet']
  69.  
  70.  
  71. @middleware
  72. def application_generator(environ, start_response):
  73.     """A simple WSGI application that use the write callable and
  74.    return a generator.
  75.    """
  76.  
  77.     def app_iter():
  78.         # write('foo')
  79.  
  80.         yield 'Test write greenlet generator\n'
  81.         for i in range(ord('a'), ord('z') + 1):
  82.             yield chr(i) * BUFSIZE + '\n'
  83.  
  84.     headers = [('Content-Type', 'text/plain')]
  85.  
  86.     write = start_response('200 OK', headers)
  87.     for i in range(0, N):
  88.         write('%02d' % i * BUFSIZE + '\n')
  89.  
  90.     return app_iter()
clone this paste RAW Paste Data