diff options
author | RaNaN <Mast3rRaNaN@hotmail.de> | 2011-08-16 20:11:57 +0200 |
---|---|---|
committer | RaNaN <Mast3rRaNaN@hotmail.de> | 2011-08-16 20:11:57 +0200 |
commit | 218ef6ec665fac20928d293e950458960f36c633 (patch) | |
tree | b2d279d6e45ac1b556db5df215ebb464333a0891 /module/Scheduler.py | |
parent | updated thrift, --clean method, youtube fix closed #379 (diff) | |
download | pyload-218ef6ec665fac20928d293e950458960f36c633.tar.xz |
plugin unloading, closed #377, some fixes
Diffstat (limited to 'module/Scheduler.py')
-rw-r--r-- | module/Scheduler.py | 40 |
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 |