龙卷风+桃子。重启已损坏的连接

时间:2014-03-22 16:26:46

标签: python django tornado psycopg2 momoko

我们正在使用龙卷风3.2 + momoko 1.1 + pgpool。

重启pgpool后 - 我们必须重启龙卷风。

如果我们不重启龙卷风 - 我们会遇到以下错误:

[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 be used while an asynchronous query is underway

我发现当使用greenlets时,django用户具有相同的troubles

如果没有龙卷风重启,如何避免这种情况?

1 个答案:

答案 0 :(得分:2)

临时解决方案 - 如果发生错误,则关闭连接。为此,让我们创建受momoko.Op启发的课程:

class OpWithFallback(gen.Task):
    """
    Run a single asynchronous operation.

    Behaves like `tornado.gen.Task`_, but raises an exception (one of Psycop2's
    exceptions_) when an error occurs related to Psycopg2 or PostgreSQL.

    Closes connection with 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 with idle transaction: %s' % result.connection)
        result.connection.close()
        raise error

让我们像使用它一样:

result = yield OpWithFallback(self.db.execute, sql, [])