summaryrefslogtreecommitdiffstats
path: root/module/Scheduler.py
diff options
context:
space:
mode:
authorGravatar mkaay <mkaay@mkaay.de> 2010-11-05 18:29:42 +0100
committerGravatar mkaay <mkaay@mkaay.de> 2010-11-05 18:29:42 +0100
commit7fababff0fb3e4251cf7ac60f7a945a9e8f13bde (patch)
treeb688b51493e5ac4418305b034676ab1c20e6c2f7 /module/Scheduler.py
parentsj fix (diff)
downloadpyload-7fababff0fb3e4251cf7ac60f7a945a9e8f13bde.tar.xz
scheduler optimization, SJOrg fix, better crypter limit
Diffstat (limited to 'module/Scheduler.py')
-rw-r--r--module/Scheduler.py76
1 files changed, 58 insertions, 18 deletions
diff --git a/module/Scheduler.py b/module/Scheduler.py
index 018b94e10..2a76bb071 100644
--- a/module/Scheduler.py
+++ b/module/Scheduler.py
@@ -17,41 +17,81 @@
@author: mkaay
"""
-from time import sleep
-from Queue import Queue
+from time import sleep, time
+from Queue import PriorityQueue, Empty
from threading import Thread
+class AlreadyCalled(Exception):
+ pass
+
+def callInThread(f, *args, **kwargs):
+ class FThread(Thread):
+ def run(self):
+ f(*args, **kwargs)
+ t = FThread()
+ t.start()
+
+class Deferred():
+ def __init__(self):
+ self.call = []
+ self.result = ()
+
+ def addCallback(self, f, *cargs, **ckwargs):
+ self.call.append((f, cargs, ckwargs))
+ if self.result:
+ args, kwargs = self.result
+ args.extend(cargs)
+ kwargs.update(ckwargs)
+ callInThread(f, *args, **kwargs)
+
+ def callback(self, *args, **kwargs):
+ if self.result:
+ raise AlreadyCalled
+ self.result = (args, kwargs)
+ for f, cargs, ckwargs in self.call:
+ args.extend(cargs)
+ kwargs.update(ckwargs)
+ callInThread(f, *args, **kwargs)
+
class Scheduler():
def __init__(self, core):
self.core = core
- self.queue = Queue()
+ self.queue = PriorityQueue()
- def addJob(self, time, call, args=[], kwargs={}, done=None):
- j = Job(time, call, args, kwargs, done)
- self.queue.put(j)
+ def addJob(self, t, call, args=[], kwargs={}):
+ d = Deferred()
+ t += time()
+ j = Job(t, call, args, kwargs, d)
+ self.queue.put((t, j))
+ return d
+
+ def work(self):
+ while True:
+ try:
+ t, j = self.queue.get(False)
+ except Empty:
+ break
+ else:
+ if t <= time():
+ j.start()
+ else:
+ self.queue.put((t, j))
class Job(Thread):
- def __init__(self, time, call, args=[], kwargs={}, done=None):
+ def __init__(self, time, call, args=[], kwargs={}, deferred=None):
Thread.__init__(self)
self.time = float(time)
- self.interval = 0.5
self.call = call
- self.done = done
+ self.deferred = deferred
self.args = args
self.kwargs = kwargs
def run(self):
- while self.time > 0:
- sleep(self.interval)
- self.time -= self.interval
- self.work()
-
- def work(self):
ret = self.call(*self.args, **self.kwargs)
- if self.done is None:
+ if self.deferred is None:
return
if ret is None:
- self.done()
+ self.deferred.callback()
else:
- self.done(ret)
+ self.deferred.callback(ret)