allow to specify arbitrary argument when recording a looping task func
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Wed, 05 Aug 2009 19:13:35 +0200
changeset 2708 60d728bdcba5
parent 2707 15ffc3c8923c
child 2709 6ee816eb9f25
allow to specify arbitrary argument when recording a looping task func
etwist/server.py
server/repository.py
server/utils.py
--- a/etwist/server.py	Wed Aug 05 19:12:10 2009 +0200
+++ b/etwist/server.py	Wed Aug 05 19:13:35 2009 +0200
@@ -36,12 +36,12 @@
     lc.start(interval)
 
 def start_looping_tasks(repo):
-    for interval, func in repo._looping_tasks:
+    for interval, func, args in repo._looping_tasks:
         repo.info('starting twisted task %s with interval %.2fs',
                   func.__name__, interval)
-        def catch_error_func(repo=repo, func=func):
+        def catch_error_func(repo=repo, func=func, args=args):
             try:
-                func()
+                func(*args)
             except:
                 repo.exception('error in looping task')
         start_task(interval, catch_error_func)
--- a/server/repository.py	Wed Aug 05 19:12:10 2009 +0200
+++ b/server/repository.py	Wed Aug 05 19:13:35 2009 +0200
@@ -309,22 +309,22 @@
 
     def start_looping_tasks(self):
         assert isinstance(self._looping_tasks, list), 'already started'
-        for i, (interval, func) in enumerate(self._looping_tasks):
-            self._looping_tasks[i] = task = LoopTask(interval, func)
+        for i, (interval, func, args) in enumerate(self._looping_tasks):
+            self._looping_tasks[i] = task = LoopTask(interval, func, args)
             self.info('starting task %s with interval %.2fs', task.name,
                       interval)
             task.start()
         # ensure no tasks will be further added
         self._looping_tasks = tuple(self._looping_tasks)
 
-    def looping_task(self, interval, func):
+    def looping_task(self, interval, func, *args):
         """register a function to be called every `interval` seconds.
 
         looping tasks can only be registered during repository initialization,
         once done this method will fail.
         """
         try:
-            self._looping_tasks.append( (interval, func) )
+            self._looping_tasks.append( (interval, func, args) )
         except AttributeError:
             raise RuntimeError("can't add looping task once the repository is started")
 
--- a/server/utils.py	Wed Aug 05 19:12:10 2009 +0200
+++ b/server/utils.py	Wed Aug 05 19:13:35 2009 +0200
@@ -96,11 +96,11 @@
 
 class LoopTask(object):
     """threaded task restarting itself once executed"""
-    def __init__(self, interval, func):
+    def __init__(self, interval, func, args):
         self.interval = interval
-        def auto_restart_func(self=self, func=func):
+        def auto_restart_func(self=self, func=func, args=args):
             try:
-                func()
+                func(*args)
             finally:
                 self.start()
         self.func = auto_restart_func