Skip to content

Instantly share code, notes, and snippets.

@flibustenet
Last active August 29, 2015 14:00
Show Gist options
  • Save flibustenet/9cf280591cdc6f684662 to your computer and use it in GitHub Desktop.
Save flibustenet/9cf280591cdc6f684662 to your computer and use it in GitHub Desktop.
tween and callbacks
from pyramid.config import Configurator
from pyramid.view import view_config
from pyramid.events import NewRequest, NewResponse, subscriber
@view_config(route_name='hello', renderer='string')
def hello(request):
print "View hello"
return 'Hello'
def response_callback(request, response):
print "response_callback"
def finish(request):
print "finished_callback"
@subscriber(NewResponse)
def new_response(event):
print "NewResponse"
@subscriber(NewRequest)
def new_request(event):
print "NewRequest"
event.request.add_response_callback(response_callback)
event.request.add_finished_callback(finish)
def tween_factory(handler, registry):
def tween(request):
print "tween_in"
res = handler(request)
print "tween_out"
return res
return tween
if __name__ == '__main__':
config = Configurator()
config.add_tween('.tween_factory')
config.add_route('hello','/')
config.scan()
app = config.make_wsgi_app()
from wsgiref.simple_server import make_server
make_server('0.0.0.0', 6543, app).serve_forever()
@flibustenet
Copy link
Author

tween_in
NewRequest
View hello
tween_out
response_callback
NewResponse
finished_callback

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment