python—访问从另一个类填充@postfork的全局变量

ajsxfq5m  于 2021-06-13  发布在  Cassandra
关注(0)|答案(0)|浏览(212)

我正在尝试访问cassandra会话,该会话在启动时由@postfork方法填充
但是我在stdout中看到“hello”,这意味着函数正在工作
Cassandra.py

cassandra_session=None

@postfork
def connect():
    print("HELLO\n\n\n")
    global cassandra_session
    cassandra_session = CassandraDatastore("keyspace")
    cassandra_session.createSession()

class CassandraDatastore(object):
    def __init__(self, keyspace):
        self.cluster = None
        self.session = None
        self.keyspace = keyspace

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

    def createSession(self):
        self.cluster = Cluster(contact_points=["localhost"], protocol_version=4,
                               control_connection_timeout=None,
                               max_schema_agreement_wait=10, load_balancing_policy=RoundRobinPolicy())
        self.session = self.cluster.connect(self.keyspace)

    def getSession(self):
        return self.session

我试着进入:

from app.client import cassandra

cassandra.cassandra_session.getSession().prepare("insert into....")

但我有个错误:

AttributeError: 'NoneType' object has no attribute 'getSession'

我遵循下面的例子:https://blog.turret.io/using-cassandra-with-python-and-uwsgi/

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题