OLD | NEW |
(Empty) | |
| 1 # copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
| 2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
| 3 # |
| 4 # This file is part of logilab-common. |
| 5 # |
| 6 # logilab-common is free software: you can redistribute it and/or modify it unde
r |
| 7 # the terms of the GNU Lesser General Public License as published by the Free |
| 8 # Software Foundation, either version 2.1 of the License, or (at your option) an
y |
| 9 # later version. |
| 10 # |
| 11 # logilab-common is distributed in the hope that it will be useful, but WITHOUT |
| 12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 13 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
| 14 # details. |
| 15 # |
| 16 # You should have received a copy of the GNU Lesser General Public License along |
| 17 # with logilab-common. If not, see <http://www.gnu.org/licenses/>. |
| 18 """Prioritized tasks queue""" |
| 19 |
| 20 __docformat__ = "restructuredtext en" |
| 21 |
| 22 from bisect import insort_left |
| 23 |
| 24 from six.moves import queue |
| 25 |
| 26 LOW = 0 |
| 27 MEDIUM = 10 |
| 28 HIGH = 100 |
| 29 |
| 30 PRIORITY = { |
| 31 'LOW': LOW, |
| 32 'MEDIUM': MEDIUM, |
| 33 'HIGH': HIGH, |
| 34 } |
| 35 REVERSE_PRIORITY = dict((values, key) for key, values in PRIORITY.items()) |
| 36 |
| 37 |
| 38 |
| 39 class PrioritizedTasksQueue(queue.Queue): |
| 40 |
| 41 def _init(self, maxsize): |
| 42 """Initialize the queue representation""" |
| 43 self.maxsize = maxsize |
| 44 # ordered list of task, from the lowest to the highest priority |
| 45 self.queue = [] |
| 46 |
| 47 def _put(self, item): |
| 48 """Put a new item in the queue""" |
| 49 for i, task in enumerate(self.queue): |
| 50 # equivalent task |
| 51 if task == item: |
| 52 # if new task has a higher priority, remove the one already |
| 53 # queued so the new priority will be considered |
| 54 if task < item: |
| 55 item.merge(task) |
| 56 del self.queue[i] |
| 57 break |
| 58 # else keep it so current order is kept |
| 59 task.merge(item) |
| 60 return |
| 61 insort_left(self.queue, item) |
| 62 |
| 63 def _get(self): |
| 64 """Get an item from the queue""" |
| 65 return self.queue.pop() |
| 66 |
| 67 def __iter__(self): |
| 68 return iter(self.queue) |
| 69 |
| 70 def remove(self, tid): |
| 71 """remove a specific task from the queue""" |
| 72 # XXX acquire lock |
| 73 for i, task in enumerate(self): |
| 74 if task.id == tid: |
| 75 self.queue.pop(i) |
| 76 return |
| 77 raise ValueError('not task of id %s in queue' % tid) |
| 78 |
| 79 class Task(object): |
| 80 def __init__(self, tid, priority=LOW): |
| 81 # task id |
| 82 self.id = tid |
| 83 # task priority |
| 84 self.priority = priority |
| 85 |
| 86 def __repr__(self): |
| 87 return '<Task %s @%#x>' % (self.id, id(self)) |
| 88 |
| 89 def __cmp__(self, other): |
| 90 return cmp(self.priority, other.priority) |
| 91 |
| 92 def __lt__(self, other): |
| 93 return self.priority < other.priority |
| 94 |
| 95 def __eq__(self, other): |
| 96 return self.id == other.id |
| 97 |
| 98 __hash__ = object.__hash__ |
| 99 |
| 100 def merge(self, other): |
| 101 pass |
OLD | NEW |