gpt4 book ai didi

python - Tornado +桃子。重新启动损坏的连接

转载 作者:行者123 更新时间:2023-12-01 05:20:05 25 4
gpt4 key购买 nike

我们正在使用tornado 3.2 + momoko 1.1 + pgpool。

重新启动 pgpool 后 - 我们必须重新启动 Tornado 。

如果我们不重新启动 Tornado - 我们会遇到如下错误:

[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

我发现 django 用户有相同的 troubles ,当使用 greenlet 时。

如何在不重新启动tornado的情况下避免这种情况?

最佳答案

临时解决方案 - 如果发生错误,请关闭连接。为此,让我们创建受 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, [])

关于python - Tornado +桃子。重新启动损坏的连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22580275/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com