summaryrefslogtreecommitdiffstats
path: root/module/Scheduler.py
diff options
context:
space:
mode:
Diffstat (limited to 'module/Scheduler.py')
-rw-r--r--module/Scheduler.py40
1 files changed, 34 insertions, 6 deletions
diff --git a/module/Scheduler.py b/module/Scheduler.py
index 5837dec9e..0bc396b69 100644
--- a/module/Scheduler.py
+++ b/module/Scheduler.py
@@ -39,23 +39,42 @@ class Deferred():
raise AlreadyCalled
self.result = (args, kwargs)
for f, cargs, ckwargs in self.call:
- args+=tuple(cargs)
+ args += tuple(cargs)
kwargs.update(ckwargs)
- f(*args **kwargs)
+ f(*args ** kwargs)
+
class Scheduler():
def __init__(self, core):
self.core = core
-
+
self.queue = PriorityQueue()
-
+
def addJob(self, t, call, args=[], kwargs={}, threaded=True):
d = Deferred()
t += time()
j = Job(t, call, args, kwargs, d, threaded)
self.queue.put((t, j))
return d
-
+
+
+ def removeJob(self, d):
+ """
+ :param d: defered object
+ :return: if job was deleted
+ """
+ index = -1
+
+ for i, j in enumerate(self.queue):
+ if j[1].deferred == d:
+ index = i
+
+ if index >= 0:
+ del self.queue[index]
+ return True
+
+ return False
+
def work(self):
while True:
t, j = self.queue.get()
@@ -68,6 +87,7 @@ class Scheduler():
self.queue.put((t, j))
break
+
class Job():
def __init__(self, time, call, args=[], kwargs={}, deferred=None, threaded=True):
self.time = float(time)
@@ -90,12 +110,20 @@ class Job():
else:
self.run()
+
class PriorityQueue():
""" a non blocking priority queue """
+
def __init__(self):
self.queue = []
self.lock = Lock()
+ def __iter__(self):
+ return iter(self.queue)
+
+ def __delitem__(self, key):
+ del self.queue[key]
+
def put(self, element):
self.lock.acquire()
heappush(self.queue, element)
@@ -108,6 +136,6 @@ class PriorityQueue():
el = heappop(self.queue)
return el
except IndexError:
- return None,None
+ return None, None
finally:
self.lock.release() \ No newline at end of file