0👍
Your way is fine until you have many concurrent connections. For example, you could come up with something like:
class Message(models.Model):
created = models.DateTimeField(auto_now_add=True, db_index=True)
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerKey(db_index=True)
content_object = GenericForeignKey('content_type', 'object_id')
class MessageQuerySet(models.QuerySet):
def dequeue(self, count):
items = (self.order_by('-created')
.select_related('content_object')
.values('content_object'))
self.filter(pk__in=items).delete()
return [item['content_object'] for item in items]
stack = Message.objects.dequeue(3)
The woes begin if you have many different processes accessing your heap. It’s fairly possible that while you’re trying to dequeue some items, another process will put an item on the top. The only right solution in that case is using message brokers like RabbitMQ or ZeroMQ.
Source:stackexchange.com