Python 3 priority queues with aging – Avoiding starvation

In the previous post (Python 3 avoid starvation in Priority Queues) I explained the starvation problem you face front when using Python priority queues. In this post I’m going to reimplement a queue that avoids the problem.
To implement the new queue class I’ll use a list a tuple with three elements: the data, the priority and the aging.

Every time a producer will enqueue a new item, the class will update the aging of the items in the queue.

The update aging method is very simple, the update will decrement the aging field, if the new aging value is zero then the priority is decremented, too.

The dequeue method is very simple, and when it is called it will update the aging, too.

the main program is as follows:

Full and working source code is available on my github at prio_queue_aging.py

Related Posts

3 thoughts on “Python 3 priority queues with aging – Avoiding starvation

Leave a Reply

Your email address will not be published. Required fields are marked *