implement priority queue

This commit is contained in:
j 2016-02-11 19:56:12 +05:30
parent 0ca89db3cd
commit 8a571a10a5

View file

@ -2,7 +2,7 @@
# vi:si:et:sw=4:sts=4:ts=4 # vi:si:et:sw=4:sts=4:ts=4
import os import os
import json import json
from queue import Queue from queue import PriorityQueue
from threading import Thread from threading import Thread
from websocket import trigger_event from websocket import trigger_event
@ -17,23 +17,23 @@ class Tasks(Thread):
def __init__(self): def __init__(self):
self._taskspath = os.path.join(settings.data_path, 'tasks.json') self._taskspath = os.path.join(settings.data_path, 'tasks.json')
self.q = Queue() self.q = PriorityQueue()
Thread.__init__(self) Thread.__init__(self)
self.daemon = True self.daemon = True
self.start() self.start()
self.queue('scan')
self.load_tasks()
def run(self): def run(self):
self.load_tasks()
self.queue('scan')
import item.scan import item.scan
from item.models import sync_metadata, get_preview, get_cover from item.models import sync_metadata, get_preview, get_cover
from user.models import export_list, update_user_peering from user.models import export_list, update_user_peering
shutdown = False shutdown = False
while not shutdown: while not shutdown:
m = self.q.get() p, m = self.q.get()
if m: if m:
if state.shutdown: if state.shutdown:
self._tasks.append(m) self._tasks.append((p, m))
else: else:
try: try:
action, data = m action, data = m
@ -86,12 +86,20 @@ class Tasks(Thread):
json.dump(self._tasks, f) json.dump(self._tasks, f)
def join(self): def join(self):
self.q.put(None) self.q.put((1000, None))
r = Thread.join(self) r = Thread.join(self)
self.save_tasks() self.save_tasks()
return r return r
def queue(self, action, data=None): def queue(self, action, data=None, priority=None):
if priority is None:
priority = 100
if action in ('getcover', 'getpreview'):
priority += 1
if action in ('import', 'export'):
priority -= 1
if action in ('peering', 'changelibrarypath'):
priority -= 2
if not state.shutdown: if not state.shutdown:
logger.debug('%s queued', action) #logger.debug('queue: %s (%s)', action, data)
self.q.put((action, data)) self.q.put((priority, (action, data)))