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. class AgingPriorityQueue(object): """ Class implementing a priority queue with aging. """ def __init__(self, qsize=32, max_aging=10): """[summary] Keyword Arguments: qsize {Integer} -- he max size for the queue (default: {32}) max_aging {Integer} -- The maximum value for the aging (default: {10}) """ self.max_aging = max_aging self.queue = list() self.queue_len = 0 self.q_size = qsizeEvery time a producer will enqueue a new item, the class will update the aging of the items in the queue.@synchro(LOCK) def enqueue(self, data, item_prio=0, aging=10): """ Enqueues a new item into the queue Decorators: synchro Synchronizes the access to the queue Arguments: data {data} -- the item to be added to the queue Keyword Arguments: item_prio {Integer} -- The priority of the item (default: {0}) aging {Integer} -- The initial aging of the item (default: {10}) Returns: Boolean -- False if the queue is full, True if the item has been inserted.
I guess you came to this post by searching similar kind of issues in any of the search engine and hope that this resolved your problem. If you find this tips useful, just drop a line below and share the link to others and who knows they might find it useful too.
Stay tuned to my blog, twitter or facebook to read more articles, tutorials, news, tips & tricks on various technology fields. Also Subscribe to our Newsletter with your Email ID to keep you updated on latest posts. We will send newsletter to your registered email address. We will not share your email address to anybody as we respect privacy.
Stay tuned to my blog, twitter or facebook to read more articles, tutorials, news, tips & tricks on various technology fields. Also Subscribe to our Newsletter with your Email ID to keep you updated on latest posts. We will send newsletter to your registered email address. We will not share your email address to anybody as we respect privacy.
This article is related to
python,Tower of Babel,priority,queue,starvation
python,Tower of Babel,priority,queue,starvation
No comments:
Post a Comment