TaskHandler.py :  » Web-Frameworks » Webware » Webware-1.0.2 » TaskKit » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Web Frameworks » Webware 
Webware » Webware 1.0.2 » TaskKit » TaskHandler.py

from time import time
from threading import Thread

try: # for Python < 2.3
  True, False
except NameError:
  True, False = 1, 0


class TaskHandler:
  """The task handler.

  While the Task class only knows what task to perform with the run()-method,
  the TaskHandler has all the knowledge about the periodicity of the task.
  Instances of this class are managed by the Scheduler in the scheduled,
  running and onDemand dictionaries.

  """


  ## Init ##

  def __init__(self, scheduler, start, period, task, name):
    self._scheduler = scheduler
    self._task = task
    self._name = name
    self._thread = None
    self._isRunning = False
    self._suspend = False
    self._lastTime = None
    self._startTime = start
    self._registerTime = time()
    self._reregister = True
    self._rerun = False
    self._period = abs(period)


  ## Scheduling ##

  def reset(self, start, period, task, reregister):
    self._startTime = start
    self._period = abs(period)
    self._task = task
    self._reregister = reregister

  def runTask(self):
    """Run this task in a background thread."""
    if self._suspend:
      self._scheduler.notifyCompletion(self)
      return
    self._rerun = False
    self._thread = Thread(None, self._task._run, self.name(), (self,))
    self._isRunning = True
    self._thread.start()

  def reschedule(self):
    """Determine whether this task should be rescheduled.

    Increments the startTime and returns true if this is
    a periodically executed task.

    """
    if self._period == 0 or not self._reregister:
      return False
    else:
      if self._lastTime - self._startTime > self._period:
        # if the time taken to run the task exceeds the period
        self._startTime = self._lastTime + self._period
      else:
        self._startTime += self._period
      return True

  def notifyCompletion(self):
    self._isRunning = False
    self._lastTime = time()
    self._scheduler.notifyCompletion(self)

  def notifyFailure(self):
    self._isRunning = False
    self._lastTime = time()
    self._scheduler.notifyFailure(self)


  ## Attributes ##

  def isRunning(self):
    return self._isRunning

  def runAgain(self):
    """Determine whether this task should be run again.

    This method lets the Scheduler check to see whether this task should be
    re-run when it terminates.

    """
    return self._rerun

  def isOnDemand(self):
    """Return True if this task is not scheduled for periodic execution."""
    return self._period == 0

  def runOnCompletion(self):
    """Request that this task be re-run after its current completion.

    Intended for on-demand tasks that are requested by the Scheduler while
    they are already running.

    """
    self._rerun = True

  def unregister(self):
    """Request that this task not be kept after its current completion.

    Used to remove a task from the scheduler.

    """
    self._reregister = False
    self._rerun = False

  def disable(self):
    """Disable future invocations of this task."""
    self._suspend = True

  def enable(self):
    """Enable future invocations of this task."""
    self._suspend = False

  def period(self):
    """Return the period of this task."""
    return self._period

  def setPeriod(self, period):
    """Change the period for this task."""
    self._period = period

  def stop(self):
    self._isRunning = False

  def name(self):
    return self._name

  def startTime(self, newTime=None):
    if newTime:
      self._startTime = newTime
    return self._startTime
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.