# HG changeset patch # User Sylvain Thénault # Date 1249492415 -7200 # Node ID 60d728bdcba5378f86857f63dc46bcbec9b6b5a4 # Parent 15ffc3c8923c064b098c025f88cfb93ce0dcc865 allow to specify arbitrary argument when recording a looping task func diff -r 15ffc3c8923c -r 60d728bdcba5 etwist/server.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) diff -r 15ffc3c8923c -r 60d728bdcba5 server/repository.py --- 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") diff -r 15ffc3c8923c -r 60d728bdcba5 server/utils.py --- 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