http://docs.pylonshq.com/pyramid/dev/narr/urldispatch.html#cleaning-up-after-a-request recommends this for running cleanup code after request processing:
from mypackage.sql import DBSession
class Cleanup:
def __init__(self, cleaner):
self.cleaner = cleaner
def __del__(self):
self.cleaner()
def handle_teardown(event):
environ = event.request.environ
environ['mypackage.sqlcleaner'] = Cleanup(DBSession.remove)
#...
config.add_subscriber('mypackage.run.handle_teardown',
'pyramid.events.NewRequest')
I'm uneasy about the __del__. If used, it would prevent people from porting
their Pyramid apps to Jython or PyPy.
Isn't there a better way? The note in that section mentions middleware
which can run cleanup code after request processing, but it's a bit
heavyweight.
Have you considered a different event to indicate end of request processing?
Marius Gedminas
--
Premature optimization is the root of all evil.
-- D.E. Knuth
signature.asc
Description: Digital signature
