sort tasks only on first element

This commit is contained in:
j 2016-02-29 18:20:56 +05:30
parent 1114f9099b
commit 6ed6e6bb95

View file

@ -15,6 +15,16 @@ logger = logging.getLogger(__name__)
DEUBG_TASKS = False DEUBG_TASKS = False
class QueueElement(tuple):
def __lt__(self, rhs):
return self[0] < rhs[0]
def __gt__(self, rhs):
return self[0] > rhs[0]
def __le__(self, rhs):
return self[0] <= rhs[0]
def __ge__(self, rhs):
return self[0] >= rhs[0]
class Tasks(Thread): class Tasks(Thread):
_tasks = [] _tasks = []
@ -81,7 +91,7 @@ class Tasks(Thread):
tasks = json.load(f) tasks = json.load(f)
for task in tasks: for task in tasks:
if len(task) == 2: if len(task) == 2:
self.q.put((task[0], tuple(task[1]))) self.q.put(QueueElement((task[0], tuple(task[1]))))
logger.debug('loaded %s tasks', len(tasks)) logger.debug('loaded %s tasks', len(tasks))
except: except:
logger.debug('failed to load saved tasks', exc_info=True) logger.debug('failed to load saved tasks', exc_info=True)
@ -94,7 +104,7 @@ class Tasks(Thread):
json.dump(self._tasks, f) json.dump(self._tasks, f)
def join(self): def join(self):
self.q.put((1000, None)) self.q.put(QueueElement((1000, None)))
r = Thread.join(self) r = Thread.join(self)
self.save_tasks() self.save_tasks()
return r return r
@ -110,6 +120,6 @@ class Tasks(Thread):
priority -= 2 priority -= 2
if not state.shutdown: if not state.shutdown:
#logger.debug('queue: %s (%s)', action, data) #logger.debug('queue: %s (%s)', action, data)
self.q.put((priority, (action, data))) self.q.put(QueueElement((priority, (action, data))))
else: else:
self._tasks.append((priority, (action, data))) self._tasks.append((priority, (action, data)))