diff --git a/plugins/python/python_plugin.c b/plugins/python/python_plugin.c index dcc8af82..780468ff 100644 --- a/plugins/python/python_plugin.c +++ b/plugins/python/python_plugin.c @@ -1053,6 +1053,16 @@ void uwsgi_python_init_apps() { #endif } + PyObject *uwsgi_dict = get_uwsgi_pydict("uwsgi"); + if (uwsgi_dict) { + up.after_req_hook = PyDict_GetItemString(uwsgi_dict, "after_req_hook"); + if (up.after_req_hook) { + Py_INCREF(up.after_req_hook); + up.after_req_hook_args = PyTuple_New(0); + Py_INCREF(up.after_req_hook_args); + } + } + } void uwsgi_python_master_fixup(int step) { diff --git a/plugins/python/uwsgi_python.h b/plugins/python/uwsgi_python.h index 97123d8a..58b578df 100644 --- a/plugins/python/uwsgi_python.h +++ b/plugins/python/uwsgi_python.h @@ -186,6 +186,9 @@ struct uwsgi_python { int reload_os_env; + PyObject *after_req_hook; + PyObject *after_req_hook_args; + }; diff --git a/plugins/python/wsgi_handlers.c b/plugins/python/wsgi_handlers.c index d19614f5..b5d1cc27 100644 --- a/plugins/python/wsgi_handlers.c +++ b/plugins/python/wsgi_handlers.c @@ -513,6 +513,17 @@ clear2: void uwsgi_after_request_wsgi(struct wsgi_request *wsgi_req) { + if (up.after_req_hook) { + PyObject *arh = python_call(up.after_req_hook, up.after_req_hook_args, 0, NULL); + if (!arh) { + PyErr_Print(); + } + else { + Py_DECREF(arh); + } + PyErr_Clear(); + } + if (uwsgi.shared->options[UWSGI_OPTION_LOGGING] || wsgi_req->log_this) { log_request(wsgi_req); } diff --git a/welcome.py b/welcome.py index 6a16f0fb..1618a26f 100644 --- a/welcome.py +++ b/welcome.py @@ -18,6 +18,10 @@ except: DEBUG = False +def after_request_hook(): + print "request finished" + +uwsgi.after_req_hook = after_request_hook def xsendfile(e, sr): sr('200 OK', [('Content-Type', 'image/png'), ('X-Sendfile', os.path.abspath('logo_uWSGI.png'))])