python - tornado + momoko doesn't handle connection -
i use tornado (4.2.1) + momoko (2.2.0) + psycopg2 (2.6.1) small web application , works ok until postgresql server close connection. after every db.execute() command receive error message:
traceback (most recent call last): file "c:\python27\lib\site-packages\tornado-4.2.1-py2.7-win32.egg\tornado\web.py", line 1415, in _execute result = yield result file "c:\python27\lib\site-packages\tornado-4.2.1-py2.7-win32.egg\tornado\gen.py", line 870, in run value = future.result() file "c:\python27\lib\site-packages\tornado-4.2.1-py2.7-win32.egg\tornado\concurrent.py", line 215, in result raise_exc_info(self._exc_info) file "c:\python27\lib\site-packages\tornado-4.2.1-py2.7-win32.egg\tornado\gen.py", line 876, in run yielded = self.gen.throw(*exc_info) file "server.py", line 63, in cursor = yield self.db.execute(query) file "c:\python27\lib\site-packages\tornado-4.2.1-py2.7-win32.egg\tornado\gen.py", line 870, in run value = future.result() file "c:\python27\lib\site-packages\tornado-4.2.1-py2.7-win32.egg\tornado\concurrent.py", line 215, in result raise_exc_info(self._exc_info) file "d:\work\program-stat\momoko\connection.py", line 453, in when_available future_or_result = method(conn, *args, **kwargs) file "d:\work\program-stat\momoko\connection.py", line 743, in execute cursor.execute(operation, parameters) file "c:\python27\lib\site-packages\psycopg2\extras.py", line 288, in execute return super(namedtuplecursor, self).execute(query, vars) operationalerror: server closed connection unexpectedly means server terminated abnormally before or while processing request.
here code:
import os import tornado.httpserver import tornado.ioloop import tornado.options import tornado.web import momoko tornadotools.route import route psycopg2.extras import namedtuplecursor import environments env tornado.options.define("port", default=9999, help="run on given port", type=int) tornado.options.define("pgsql_host", default=env.db_host, help="database host") tornado.options.define("pgsql_database", default=env.db_database, help="database name") tornado.options.define("pgsql_user", default=env.db_login, help="database user") tornado.options.define("pgsql_password", default=env.db_password, help="database password") class application(tornado.web.application): def __init__(self): handlers = route.routes() settings = dict( template_path=os.path.join(os.path.dirname(__file__), "templates"), static_path=os.path.join(os.path.dirname(__file__), "static"), debug=true, ) tornado.web.application.__init__(self, handlers, **settings) # have 1 global connection db across handlers self.db = momoko.pool( dsn='dbname=%s user=%s password=%s ' 'host=%s port=5432' % ( tornado.options.options.pgsql_database, tornado.options.options.pgsql_user, tornado.options.options.pgsql_password, tornado.options.options.pgsql_host), cursor_factory=namedtuplecursor, ) self.db.connect() class basehandler(tornado.web.requesthandler): @property def db(self): return self.application.db @route(r"/") class homehandler(basehandler): def get(self): self.write("<ul>") self.write("<li><a href='/test'>test page</a>") self.write("</ul>") self.finish() @route(r"/test") class mogrifyhandler(basehandler): @tornado.web.asynchronous @tornado.gen.coroutine def get(self): cursor = yield self.db.execute("select %s t;", (1,)) self.render("test.html", result=cursor.fetchall()) def main(): tornado.options.parse_command_line() http_server = tornado.httpserver.httpserver(application()) http_server.listen(tornado.options.options.port) tornado.ioloop.ioloop.instance().start() if __name__ == "__main__": main()
how handle exception , reconnect db automatically without restarting app?
you can add parameter:
raise_connect_errors=false,
when create connection pool:
self.db = momoko.pool( dsn='dbname=%s user=%s password=%s ' 'host=%s port=5432' % ( tornado.options.options.pgsql_database, tornado.options.options.pgsql_user, tornado.options.options.pgsql_password, tornado.options.options.pgsql_host), cursor_factory=namedtuplecursor, raise_connect_errors=false, )
but not helpful.
Comments
Post a Comment