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 from Queue import Queue |
| 24 |
| 25 LOW = 0 |
| 26 MEDIUM = 10 |
| 27 HIGH = 100 |
| 28 |
| 29 PRIORITY = { |
| 30 'LOW': LOW, |
| 31 'MEDIUM': MEDIUM, |
| 32 'HIGH': HIGH, |
| 33 } |
| 34 REVERSE_PRIORITY = dict((values, key) for key, values in PRIORITY.iteritems()) |
| 35 |
| 36 |
| 37 |
| 38 class PrioritizedTasksQueue(Queue): |
| 39 |
| 40 def _init(self, maxsize): |
| 41 """Initialize the queue representation""" |
| 42 self.maxsize = maxsize |
| 43 # ordered list of task, from the lowest to the highest priority |
| 44 self.queue = [] |
| 45 |
| 46 def _put(self, item): |
| 47 """Put a new item in the queue""" |
| 48 for i, task in enumerate(self.queue): |
| 49 # equivalent task |
| 50 if task == item: |
| 51 # if new task has a higher priority, remove the one already |
| 52 # queued so the new priority will be considered |
| 53 if task < item: |
| 54 item.merge(task) |
| 55 del self.queue[i] |
| 56 break |
| 57 # else keep it so current order is kept |
| 58 task.merge(item) |
| 59 return |
| 60 insort_left(self.queue, item) |
| 61 |
| 62 def _get(self): |
| 63 """Get an item from the queue""" |
| 64 return self.queue.pop() |
| 65 |
| 66 def __iter__(self): |
| 67 return iter(self.queue) |
| 68 |
| 69 def remove(self, tid): |
| 70 """remove a specific task from the queue""" |
| 71 # XXX acquire lock |
| 72 for i, task in enumerate(self): |
| 73 if task.id == tid: |
| 74 self.queue.pop(i) |
| 75 return |
| 76 raise ValueError('not task of id %s in queue' % tid) |
| 77 |
| 78 class Task(object): |
| 79 def __init__(self, tid, priority=LOW): |
| 80 # task id |
| 81 self.id = tid |
| 82 # task priority |
| 83 self.priority = priority |
| 84 |
| 85 def __repr__(self): |
| 86 return '<Task %s @%#x>' % (self.id, id(self)) |
| 87 |
| 88 def __cmp__(self, other): |
| 89 return cmp(self.priority, other.priority) |
| 90 |
| 91 def __lt__(self, other): |
| 92 return self.priority < other.priority |
| 93 |
| 94 def __eq__(self, other): |
| 95 return self.id == other.id |
| 96 |
| 97 def merge(self, other): |
| 98 pass |
OLD | NEW |