Flask and Web.py both hang on atexit

I have this simple Flask application:

from flask import Flask
import prolog_handler as p

app = Flask(__name__)
app.debug = False

@app.route('/')
def hello():
    for 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 a session with a tripost and loads some rules. It also has an atexit function that terminates the session and prints a message like "Close ...". I start the server from a bash prompt with python myapp.py. Whenever I press CTRL-C to stop the server, nothing happens. I do not go back to the bash prompt, and I do not see the message "Closing ...". I also tried to do this using Web.py with the same results.

What prolog_handler does is literally as simple as this:

tstore = openPrologSession()
rules = ...

def cleanUp():
    print "Closing..."
    tstore.endSession()

atexit.register(cleanUp)

So why is it so hard to just complete the atexit task?

PS: , Prolog , , "...", "...", CTRL-C, bash. , . atexit, ?

+5
1

, Flask :

# These functions should be called when you tear down the application
app.teardown_functions = []

def teardown_applications(): 
    for func in app.teardown_functions:
       print('Calling teardown function %s' % func.__name__)
        func()

app.teardown_functions.append(function_tocall_at_exit)

, . gevent

if __name__ == '__main__':
    gevent.signal(signal.SIGINT, teardown_applications)
    http_server = WSGIServer(('', 5000), app)
    http_server.serve_forever()

.

:

from flask import Flask
from gevent.wsgi import WSGIServer
import gevent
import signal

from gevent import monkey
monkey.patch_all()
+6

All Articles