0

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.

CodeSamurai777
  • 3,285
  • 2
  • 24
  • 42

1 Answers1

1

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.

Michael Merickel
  • 23,153
  • 3
  • 54
  • 70
  • If you wouldn't mind pointing out a few of them in the context of pyramid I would be grateful – CodeSamurai777 Jun 27 '20 at 19:50
  • Globals are bad in almost every type of programming, albeit generally necessary in embedded domains. Globals are a type of side effect (state mutated/handled outside of the function signature) and make apis harder to use / understand. The lifecycle of a global is almost never global and thus there are times you aren't allowed to call certain functions and are reliant on documentation to make that clear. – Michael Merickel Jun 28 '20 at 17:28