One of the easiest queues to implement in Django is Django-Q.
It is straightforward to tell it to use your database for its management, rather than needing to go to Redis or Celery (although these options are available).
You’ll find that for most applications, Django-Q is all you need.
I routinely install it and configure it to use the site’s database like so:
# Django-Q
Q_CLUSTER = {
'name': 'DjangORM',
'workers': 4,
'timeout': 90,
'retry': 120,
'queue_limit': 50,
'bulk': 10,
'orm': 'default'
}
How busy is the queue?
I have an application that makes many calls to a 3rd party API.
None of these calls need to happen immediately so I can safely pass them off to a queue to process in a separate ‘thread’ than the main user interface.
Consider the case where we need to find out how many items are waiting to be processed in our queue.
We may be checking for load – or wanting to raise an error if there are too many items waiting.
A quick look at the source code finds the OrmQ
model.
You can then get the number of items in the queue using standard Django ORM calls:
from django_q.models import OrmQ
items_queued = OrmQ.objects.all().count()
So really quite straightforward in the end – once you find out about the OrmQ
model. I couldn’t find any documentation about this anywhere – so I wrote this blog post.
Note, this only works if you are using your local database for the Django-Q – if you are using Redis or Celery then this code won’t work for you.
Why not combine the above code to give the queue count and a nice plugin like Django Unicorn – and you can have a nice real time view of how many items in your queue!
Photo by Photo by Magda Ehlers.