如何配置Pyramid + Cassandra + uWSGI(分叉模式)

时间:2016-09-19 01:27:19

标签: python-3.x cassandra pyramid uwsgi cassandra-python-driver

我正在使用Python 3.5 开发金字塔 1.7 网络应用,uWSGI 2.0.11 和SQLAlchemy 1.0.9 。似乎在使用带有多个worker we should use a uWSGI postfork function的uWSGI连接到Cassandra集群时,确保每个fork将使用与池的独立连接。我尝试了以下Pyramid实现(文件my_app/__init__.py

#my_app/__init__.py
def main(global_config, **settings):

    try:
        from uwsgidecorators import postfork
    except ImportError:
        # We're not in a uWSGI context, no need to hook dbs connection
        # to the postfork event.
        connection.setup(
                     [settings['cassandra.host']],
                     settings['cassandra.keyspace'],
                     port=int(settings['cassandra.port'])
        )

    else:
        @postfork
        def init():
            """ Initialize dbs connexions in the context.
                Ensures that a new connexion 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(
                     [settings['cassandra.host']],
                     settings['cassandra.keyspace'],
                     port=int(settings['cassandra.port'])
            )


    config = Configurator(settings=settings, root_factory=my_factory)
    config.scan()
    return config.make_wsgi_app() 

但是在使用uWSGI在生产环境中运行应用程序时,我正在获得连接超时。如果您安装 libev 库,Cassandra将检测库,默认情况下使用它。但是我不知道是否需要在uWSGI中进行一些更改才能兼容(必须禁用Monkeypatching)。

这是使用the uWSGI forking mode配置Pyramid + Cassandra + uWSGI的正确方法吗?或者我错过了什么?

0 个答案:

没有答案