I am developing a Pyramid 1.7 web app using Python 3.5, uWSGI 2.0.11 and SQLAlchemy 1.0.9. Seems like when using uWSGI with multiple workers we should use a uWSGI postfork function to connect to the Cassandra cluster ensuring each fork will use a independent connection to the pool. I have tried the following Pyramid implementation (file 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()
But I am getting connections timeouts when running the app in production using uWSGI. If you install the libev library Cassandra will detect the library an use it by default. But I don't know if some changes need to be done in uWSGI in order to be compatible (Monkeypatching must be disabled for sure).
Is this the right way to configure Pyramid + Cassandra + uWSGI using the uWSGI forking mode? Or am I missing something?