I have a django app that uses redis-queue for managing long running tasks in the background. I have it set up and running (if in a stupid configuration), but I'm unclear what the appropriate namespace to store my queue is.
I set up the rq worker as recommended in the docs:
#rqsetup.py import os import redis from rq import Worker, Queue, Connection listen = ['high', 'default', 'low'] redis_url = os.getenv('REDISTOGO_URL', 'redis://localhost:6379') conn = redis.from_url(redis_url) if __name__ == '__main__': with Connection(conn): worker = Worker(map(Queue, listen)) worker.work()
I can add the queue to my view:
# views.py from rqsetup import conn from rq import Queue from somewhere import bgtask def myview(request): q = Queue(connection=conn) job = q.enqueue(bgtask) return render(request, 'somepage.html')
This is obviously dumb, because then the queue and the job are lost after the view is returned. The whole point is to have the queue in some namespace where I can access it again later.
However, I can't figure anywhere else to put it. If I try to in rqsetup
, I just get import errors, or it imports out of order before its set up properly. I don't really have a good guess of where else it should go.
I otherwise have a normally structured app:
myproject -myproject -__init__.py -settings.py -urls.py -wsgi.py -myapp -apps.py -somewhere.py -views.py -manage.py -Procfile -requirements.txt -rqsetup.py
Where should a redis-queue queue be stored in a django project?
1 Answers
Answers 1
Turns out django_rq
provides the queue namespace for you! settings.py
should have something like this:
RQ_QUEUES = { 'default': { 'HOST': 'localhost', 'PORT': 6379, 'DB': 0, 'PASSWORD': 'some-password', 'DEFAULT_TIMEOUT': 360, }, 'high': { 'URL': os.getenv('REDISTOGO_URL', 'redis://localhost:6379/0'), # If you're on Heroku 'DEFAULT_TIMEOUT': 500, }, 'low': { 'HOST': 'localhost', 'PORT': 6379, 'DB': 0, } }
Then you just just get the queue from the django_rq namespace: queue = django_rq.get_queue('high')
.
0 comments:
Post a Comment