Advertisement
eswald

Closing WSGI Middleware

Oct 16th, 2012
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # See http://blog.dscpl.com.au/2012/10/obligations-for-calling-close-on.html
  2. class ClosingMiddleware(object):
  3.     def __init__(self, application):
  4.         self.application = application
  5.         self.iterable = None
  6.  
  7.     def __call__(self, environ, start_response):
  8.         self.iterable = self.application(environ, start_response)
  9.         return self
  10.  
  11.     def __iter__(self):
  12.         for data in self.iterable:
  13.             yield data
  14.  
  15.     def close(self):
  16.         if hasattr(self.iterable, 'close'):
  17.             self.iterable.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement