-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Question: Why creating a new StrictRedis object for each request? #3
Comments
That may well be the case. I seem to remember the main client is thread safe whilst a pipeline instance isn't. Perhaps one way to configure with a single client would be to cache a configured client in the registry. |
A pipeline or PubSub instance itself can indeed not be passed between threads, but that should not be a reason not to create multiple StrictRedis instances. |
Makes sense -- happy to take a PR! |
The simplest implementation I can see is to just replace init.py with this: import redis
from .config import DEFAULT_SETTINGS
def includeme(config):
settings = DEFAULT_SETTINGS.copy()
settings = config.get_settings()
for key, value in DEFAULT_SETTINGS.items():
settings.setdefault(key, value)
settings['redis.max_connections'] = int(settings['redis.max_connections'])
pool = redis.BlockingConnectionPool.from_url(settings['redis.max_connections'],
db=settings['redis.max_connections'])
r = redis.StrictRedis.from_url(connection_pool=pool)
config.add_request_method(lambda request: r, 'redis', reify=True) along with changing the default for |
Please note that this changes the API a little bit: instead of redus.unix_socket_path you will need to use |
Re-reading the code in hooks.py I'm definitely feeling embarrassed by how overly complex / obfuscated it is. However, I'd be loathe to break anything that depends on the current implementation. So... either perhaps a more conservative approach might be to just swap this line: config.add_request_method(self.get_redis, 'redis', reify=True) For: r = self.get_redis(config)
get_redis = lambda request: r
config.add_request_method(get_redis, 'redis', reify=True) That way the behaviour should stay exactly the same, aside from the overhead of instantiating every request? |
I'ld like to use the |
The connection pool is initialized at application configuration, that's the most important part for performance.
However, my understanding is that the StrictRedis class is thread-safe and could also be created once during application configuration and shared across requests.
Is there a reason to this?
The text was updated successfully, but these errors were encountered: