python - Tornado + Momoko. Restart corrupted connection -
we're using tornado 3.2 + momoko 1.1 + pgpool.
after restarting pgpool - have restart tornado.
if don't restart tornado - have errors these:
[e 140322 19:23:32 web:1305] uncaught exception post /api/user/balance/ (127.0.0.1) httprequest(protocol='http', host='127.0.0.1:3001', method='post', uri='/api/user/balance/', version='http/1.0', remote_ip='127.0.0.1', headers={'host': '127.0.0.1:3001', 'connection': 'close', 'content-type': 'application/x-www-form-urlencoded', 'content-length': '55', 'accept-encoding': 'gzip'}) traceback (most recent call last): file "/projects/application_new/env/lib/python2.7/site-packages/tornado/web.py", line 1192, in _stack_context_handle_exception raise_exc_info((type, value, traceback)) file "/projects/application_new/env/lib/python2.7/site-packages/tornado/web.py", line 1375, in wrapper result = method(self, *args, **kwargs) file "api_server.py", line 435, in post userbalanceforjson(self.db, user_id=user_id).results(self.json_callback) file "/projects/application_new/env/lib/python2.7/site-packages/tornado/gen.py", line 159, in wrapper runner.run() file "/projects/application_new/env/lib/python2.7/site-packages/tornado/gen.py", line 529, in run yielded = self.gen.throw(*exc_info) file "/projects/application_new/api/models/orm.py", line 16, in results result = yield momoko.op(self.db.execute, sql, []) file "/projects/application_new/env/lib/python2.7/site-packages/tornado/gen.py", line 520, in run next = self.yield_point.get_result() file "/projects/application_new/env/lib/python2.7/site-packages/momoko/utils.py", line 41, in get_result raise error programmingerror: execute cannot used while asynchronous query underway
i've found django users have same troubles, when using greenlets.
how avoid such situation without tornado restart?
temporary solution - close connection, if error happened in it. it, let's create class inspired momoko.op
:
class opwithfallback(gen.task): """ run single asynchronous operation. behaves `tornado.gen.task`_, raises exception (one of psycop2's exceptions_) when error occurs related psycopg2 or postgresql. closes connection error. .. _exceptions: http://initd.org/psycopg/docs/module.html#exceptions .. _tornado.gen.task: http://www.tornadoweb.org/documentation/gen.html#tornado.gen.task """ def get_result(self): (result, error), _ = super(opwithfallback, self).get_result() if error: self.fallback(error, result) return result def fallback(self, error, result): log.warning('closing dead connection or connection idle transaction: %s' % result.connection) result.connection.close() raise error
and let's use like:
result = yield opwithfallback(self.db.execute, sql, [])
Comments
Post a Comment