python - Flask and Web.py both hang on atexit -
i have simple flask app:
from flask import flask import prolog_handler p app = flask(__name__) app.debug = false @app.route('/') def hello(): rule in p.rules: print rule return 'hello' if __name__ == '__main__': app.run(host='0.0.0.0', port=8080) the prolog_handler module starts session triplestore , loads rules. has atexit function ends session , prints message "closing...". start server bash prompt python myapp.py. whenever hit ctrl-c stop server, nothing happens. don't returned bash prompt, , don't see "closing..." message printed. tried web.py same results.
the prolog_handler literally simple this:
tstore = openprologsession() rules = ... def cleanup(): print "closing..." tstore.endsession() atexit.register(cleanup) so why difficult perform atexit task?
ps: if comment out stuff opening prolog session , ending it, , leave part prints message "closing..." see "closing..." message when hit ctrl-c , returned bash prompt. works expected. what's point of atexit if can't useful it?
this may not perfect answer tried use following flask:
# these functions should called when tear down application app.teardown_functions = [] def teardown_applications(): func in app.teardown_functions: print('calling teardown function %s' % func.__name__) func() app.teardown_functions.append(function_tocall_at_exit) this seems work me. tend use gevent flask applications
if __name__ == '__main__': gevent.signal(signal.sigint, teardown_applications) http_server = wsgiserver(('', 5000), app) http_server.serve_forever() this works me.
some of module imports:
from flask import flask gevent.wsgi import wsgiserver import gevent import signal gevent import monkey monkey.patch_all()
Comments
Post a Comment