python - cassandra 增加了 uwsgi 服务器中的操作超时

标签 python cassandra uwsgi gunicorn

我有一个用 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/

相关文章:

python - AIOHTTP - Application.make_handler(...) 已弃用 - 添加多处理

cassandra - cassandra 升级是否需要运行 nodetool upgradesstables 用于集群保存 TTLed 数据

cassandra - 原子批处理在Cassandra中如何工作?

uWSGI 上的 flask-socketio

python - 如何使用 Python 线程更快地写入大量小文件

python - 断言对模拟方法的连续调用

java - 使用 Astyanax 将 Cassandra 查询结果转换为 POJO

python - 运行多个 uwsgi python 版本

ubuntu - 如何使用 apt-get 在 Ubuntu 上安装最新的 uWSGI/stable

Python 术语 : things to left of "= argv" in Learn Python the Hard Way exercise 13