我有一个用 python 编写的 Web 服务器,它使用 Cassandra 的 python 驱动程序与 cassandra 数据库进行交互。 当我使用gunicorn http服务器启动这个python服务器时,我的请求处理没有错误。 但是当我在第一个请求后使用 uwsgi http 服务器运行同一服务器时,必须将一些数据写入 Cassandra 表中,cassandra 会引发错误
cassandra.OperationTimedOut: errors={}, last_host=127.0.0.1
在 python 中调用 session.prepare() 函数时出现错误。
最佳答案
我们在应用程序中收到了相同的错误消息。
我们通过在构造函数中打开 Cassandra session 并在模型类的销毁函数中关闭它来修复它。请参阅下面的代码
class Model():
def __init__(self):
self.db = cassandra.createSession()
def __del__(self):
self.db.shutdown()
编辑: 我在这里找到了更好的解决方案:uWSGI Cassandra
from cqlengine import connection
from cqlengine.connection import (
cluster as cql_cluster, session as cql_session)
try:
from uwsgidecorators import postfork
except ImportError:
# We're not in a uWSGI context, no need to hook Cassandra session
# initialization to the postfork event.
pass
else:
@postfork
def cassandra_init():
""" Initialize a new Cassandra session in the context.
Ensures that a new session is returned for every new request.
"""
if cql_cluster is not None:
cql_cluster.shutdown()
if cql_session is not None:
cql_session.shutdown()
connection.setup()
关于python - cassandra 增加了 uwsgi 服务器中的操作超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30846525/