[kupfer: 23/27] bg_loader: allow to start and stop background task
- From: Ulrik Sverdrup <usverdrup src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [kupfer: 23/27] bg_loader: allow to start and stop background task
- Date: Tue, 16 Feb 2010 20:49:03 +0000 (UTC)
commit 9b7965319051c84011c6002e6a1a5f8edfdd3c1c
Author: Karol BÄ?dkowski <karol bedkowsk+gh gmail com>
Date: Tue Feb 9 21:58:40 2010 +0100
bg_loader: allow to start and stop background task
Intention: start / stop backgroud job after enable / disable plugin
kupfer/obj/helplib.py | 6 ++++++
kupfer/task.py | 21 ++++++++++++++++-----
2 files changed, 22 insertions(+), 5 deletions(-)
---
diff --git a/kupfer/obj/helplib.py b/kupfer/obj/helplib.py
index d00b69d..8c1f0f3 100644
--- a/kupfer/obj/helplib.py
+++ b/kupfer/obj/helplib.py
@@ -185,5 +185,11 @@ class _BackgroundLoader (object):
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 2e65412..246e5ed 100644
--- a/kupfer/task.py
+++ b/kupfer/task.py
@@ -126,9 +126,14 @@ class BackgroundTaskRunner(pretty.OutputMixin):
self.result_callback = None
self.error_callback = None
self.next_run_timer = scheduler.Timer()
+ self._active = False
def start(self):
- ''' Start thread. If task is not ready - only mark as active. '''
+ ''' 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
@@ -136,16 +141,16 @@ class BackgroundTaskRunner(pretty.OutputMixin):
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_info('Start task', self.name,
- 'delay:', self.startup_delay,
- 'interval:', self.interval)
- # get data
+ self.output_debug('_run task', self.name)
task = _BackgroundTask(self._job, self.name, self.result_callback,
self.error_callback)
task.start(self._task_finished)
@@ -155,3 +160,9 @@ class BackgroundTaskRunner(pretty.OutputMixin):
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]