]> code.delx.au - bg-scripts/blobdiff - lib/asyncsched.py
Removed a bunch of hard coded paths (on my old machine)
[bg-scripts] / lib / asyncsched.py
index c021fc108be8d49d1ef3fdc7c8efec9f4c84cc1d..428fcf3fb39dcbfd710bb3653da880eefb085b9c 100644 (file)
@@ -3,10 +3,12 @@
 # asyncore.loop() with delayed function calls
 
 import asyncore
-import time
 import heapq
+import signal
+import time
 
 tasks = []
+running = False
 
 class Task(object):
        def __init__(self, delay, func, args=[], kwargs={}):
@@ -31,8 +33,12 @@ def schedule(delay, func, args=[], kwargs={}):
        heapq.heappush(tasks, task)
        return task
 
-def loop(timeout=30.0):
-       while True:
+def loop(timeout=30.0, use_poll=False):
+       global running
+       running = True
+       oldhandler = signal.signal(signal.SIGTERM, exit)
+
+       while running:
                now = time.time()
                while tasks and tasks[0].time < now:
                        task = heapq.heappop(tasks)
@@ -42,5 +48,13 @@ def loop(timeout=30.0):
                if tasks:
                        t = max(min(t, tasks[0].time - now), 0)
 
-               asyncore.poll(timeout=t)
+               asyncore.loop(timeout=t, count=1, use_poll=use_poll)
+       
+       signal.signal(signal.SIGTERM, oldhandler)
+
+def exit(*args):
+       global running
+       running = False
+
+__all__ = ("schedule", "loop", "exit")