[kupfer] Remove BackgroundTaskRunner and background_loader
- From: Ulrik Sverdrup <usverdrup src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [kupfer] Remove BackgroundTaskRunner and background_loader
- Date: Sun, 9 May 2010 15:55:08 +0000 (UTC)
commit 1e7315410d6eb1a84336ec069628c3e5fee39d7d
Author: Ulrik Sverdrup <ulrik sverdrup gmail com>
Date: Sun May 9 16:25:25 2010 +0100
Remove BackgroundTaskRunner and background_loader
kupfer/obj/helplib.py | 68 ----------------------------------------
kupfer/task.py | 82 -------------------------------------------------
2 files changed, 0 insertions(+), 150 deletions(-)
---
diff --git a/kupfer/obj/helplib.py b/kupfer/obj/helplib.py
index a90441c..001511c 100644
--- a/kupfer/obj/helplib.py
+++ b/kupfer/obj/helplib.py
@@ -126,71 +126,3 @@ def reverse_action(action, rank=0):
ReverseAction.__name__ = "Reverse" + action.__name__
return ReverseAction
-
-def background_loader(interval, delay=5, name=None, update_empty=False):
- """ Run decorated function in background and cache it result.
-
- @interval: time between runs function in seconds
- @delay: optional delay to run background process in second
- @name: optional name of the thread (for logging)
- @update_empty: when function return empty result we may decide is cached
- result should be updated or not. When update_empty==True result always
- is stored.
-
- Example:
- @background_loader(interval=600)
- def load():
- return list(<items>)
-
- Background process should be started by:
- load.bind_and_start(<optional callback>))
- - callback is launched after each update
- or:
- load.start()
-
- If cache should be initially filled this may be done with:
- load.fill_cache(<value>)
-
- To force run background job call:
- load.activate()
- """
- return functools.partial(_BackgroundLoader, interval=interval,
- delay=delay, name=name, update_empty=update_empty)
-
-class _BackgroundLoader (object):
- def __init__(self, func, **kwargs):
- self.cache = []
- self.update_empty = kwargs.pop("update_empty", False)
- self.job = task.BackgroundTaskRunner(func, **kwargs)
- self.new_data_callback = None
- self.job.result_callback = self._result_callback
- self.job.error_callback = self._error_callback
-
- def _result_callback(self, data):
- if data or self.update_empty:
- self.cache[:] = data
- if self.new_data_callback:
- self.new_data_callback()
-
- def _error_callback(self, exc_info):
- pretty.print_exc(__name__, exc_info)
-
- def fill_cache(self, data):
- if data:
- self.cache[:] = data
-
- def bind_and_start(self, callback):
- self.new_data_callback = callback
- self.job.start()
-
- def activate(self):
- self.job.activate()
-
- def stop(self):
- self.job.stop()
-
- def start(self):
- self.job.start()
-
- def __call__(self):
- return iter(self.cache)
diff --git a/kupfer/task.py b/kupfer/task.py
index ff13228..f4ffe2c 100644
--- a/kupfer/task.py
+++ b/kupfer/task.py
@@ -85,85 +85,3 @@ class TaskRunner (pretty.OutputMixin):
self.output_info("Uncompleted tasks:")
for task in self.tasks:
self.output_info(task)
-
-
-class _BackgroundTask (ThreadTask):
- def __init__(self, job, name, result_callback, error_callback):
- ThreadTask.__init__(self, name)
- self.job = job
- self.result_callback = result_callback
- self.error_callback = error_callback
- self.error_occurred = True
-
- def thread_do(self):
- ret = self.job()
- self.error_occurred = False
- gobject.idle_add(self.result_callback, ret)
-
- def thread_finally(self, exc_info):
- if exc_info:
- self.error_callback(exc_info)
-
-
-class BackgroundTaskRunner(pretty.OutputMixin):
- """ Background job for load some data and cache it. """
-
- def __init__(self, job, interval, delay=10, name=None):
- """ Constr.
-
- @job: function to run
- @interval: time between reloading data (run job function, in seconds)
- @delay: startup delay in second
- @name: name of thread
- """
- self.name = name or repr(job)
- # function to run
- self._job = job
- self.interval = interval
- # optional interval after error running job
- self.interval_after_error = interval
- self.startup_delay = delay
- # function called after run job
- self.result_callback = None
- self.error_callback = None
- self.next_run_timer = scheduler.Timer()
- self._active = False
-
- def start(self):
- ''' Start thread.'''
- self.output_info('Start task', self.name,
- 'delay:', self.startup_delay,
- 'interval:', self.interval)
- self._active = True
- self.next_run_timer.set(self.startup_delay, self._run)
-
- @property
- def is_running(self):
- return not self.next_run_timer.is_valid()
-
- def _task_finished(self, task):
- if not self._active:
- # task is stoped
- return
- # wait for next run
- interval = (self.interval_after_error if task.error_occurred
- else self.interval)
- self.next_run_timer.set(interval, self._run)
-
- def _run(self):
- self.output_debug('_run task', self.name)
- task = _BackgroundTask(self._job, self.name, self.result_callback,
- self.error_callback)
- task.start(self._task_finished)
-
- def activate(self):
- ''' Force run job (break waiting phase). '''
- if not self.is_running:
- self.next_run_timer.set(0, self._run)
-
- def stop(self):
- ''' Stop background task '''
- self.output_info('Stop task', self.name)
- self._active = False
- self.next_run_timer.invalidate()
-
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]