cassandra rises operation time out in uwsgi server

586 views Asked by At

I have a web server written in python, which is interacting with cassandra database using Cassandra's python driver. when i starting this python server using gunicorn http server my requests are handled without error. but when i run the same server using uwsgi http server after firs request, which must write some data into Cassandra table, cassandra raises an error

cassandra.OperationTimedOut: errors={}, last_host=127.0.0.1

the error raises in session.prepare() funcion call in python.

1

There are 1 answers

0
Nguyen Sy Thanh Son On BEST ANSWER

We got the same error message in our application.

And we fixed it by opening Cassandra session in constructor function, and shutdown it in destroy function in Model Class. Please see code below

class Model():
      def __init__(self):
          self.db = cassandra.createSession()

      def __del__(self):
          self.db.shutdown()

EDITED: I found a better solution here: 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()