Search code examples
pythonpyramidredis-py

Connection pool to external services (redis) in pyramid


I need to create a global connection pool to redis redis.BlockingConnectionPool what would be the best way to initialize this and where to do it. So I can always have access to to them and those connections can always be open for very fast access. I found two approaches:

  1. Using config registry as such
#__init__.py
def make_wsgi_app(settings, **kwargs):
     ...
    config = Configurator(settings=settings)
    config.registry.connection_pool = redis.BlockingConnectionPool()
  1. Using global module constants
#myredis.py
RedisConnectionPool = BlockingConnectionPool()
#__init__.py
import myredis
def make_wsgi_app(settings, **kwargs):
     ...

I am new to pyramid and don't really know how this pool would be shared in each case and how they differ. Any insight would be greatly appreciated.

Just to be clear redis will NOT be used as session backend etc. It would be used as a kind of IPC. Where jobs would be scheduled, and where their result would be stored.


Solution

  • The recommended approach would be to store a connection pool on the registry. Each request can then grab a connection from there via request.registry.connection_pool and do its thing. This is always preferable versus using the global module for all the standard reasons global variables are bad.