Open riclm opened 2 years ago
I'm just butting in, as I've been using this with Django 4, but what kind of error would we be seeing? Asking, as I keep encountering an improperly configured database error, saying I need to pass in engine
, despite my database being configured properly
My point is: getting a client connection from Django's cache API is probably simpler. Most of the details (connection pools, redis client instances, etc.) have already been taken care of. Even when considering backwards compatibility, we can still change some stuff to mimic some parts of django's redis backend.
Yes, I'm all for this. If someone can open a PR to support using the same Redis connection from Django's cache backend, I'm happy to review and merge it in.
Django 4 has added support for caching with Redis:
I know not everyone can take advantage of this right away, but this might be helpful for people like me who are starting a new project today.
So, after reading the source code django/core/cache/backends/redis.py, I can say that the integration django<->redis-py is quite elegant. I'm not 100% sure, and that's why I'm hoping to get some feedback from you guys, but this new cache backend kind of solves some issues.
For instance, issue #210, _get_redisconnection() creates new redis client every time it is called, and according to redis-py ~ Connection Pools:
I know we can just:
In order to avoid creating new connections and controlling client instances.
However, we can make this easier by using Django's new backend:
Note that the
_cache
method has the cached_property decorator, which, as far as I can tell, will return the same instance every time it's called. This seems nicer than dealing with a singleton and instantiating a redis client directly liker = redis.Redis(...)
. Django already takes care of almost everything via the Cache API.Here's how my settings file looks:
My point is: getting a client connection from Django's cache API is probably simpler. Most of the details (connection pools, redis client instances, etc.) have already been taken care of. Even when considering backwards compatibility, we can still change some stuff to mimic some parts of django's redis backend.
Also, the
get_redis_connection()
function from the queues module does not work with Django's new redis backend IF we try to use theUSE_REDIS_CACHE
config.Fixing this to add support for Django 4 would be pretty simple since we can just do
caches["name_of_my_redis_backend"]._cache.get_client(...)
. Everything else could remain the same to maintain compatibility.Another thing that caught my attention was this: persistent database connections? #228.
It seems Django-rq uses the
reset_db_connections()
function to close all database connections viadjango.db.connections
-- rqworker.py.Why is this necessary? I mean, let's say I'm running some Docker containers, one for each -- Redis, Postgres, and Django --, why must I close all database connections? Why do postgres connections impact redis connections?
I don't fully understand everything that Django-rq (or even RQ) does, so I'm guessing that there's a good reason for that. Could you guys clue me in?
@selwin mentioned:
But if you guys could elaborate a bit.. it would be nice.
Django's redis backend also handles connection pools via the
_get_connection_pool()
method when weget_client()
. So, this seems like it could improve the overall architecture of Django-rq?Thank you!