Scheduler

This is the task manager Python package.

It provides a system for running any number of predefined tasks in separate threads in an organized and controlled manner.

A task in this package is a class derived from the Task class. The task should have a run method that, when called, performs some task.

The Scheduler class is the organizing object. It manages the addition, execution, deletion, and well being of a number of tasks. Once you have created your task class, you call the Scheduler to get it added to the tasks to be run.

class TaskKit.Scheduler.Scheduler(daemon=True, exceptionHandler=None)

Bases: Thread

The top level class of the task manager system.

The Scheduler is a thread that handles organizing and running tasks. The Scheduler class should be instantiated to start a task manager session. Its start method should be called to start the task manager. Its stop method should be called to end the task manager session.

__init__(daemon=True, exceptionHandler=None)

This constructor should always be called with keyword arguments. Arguments are:

group should be None; reserved for future extension when a ThreadGroup class is implemented.

target is the callable object to be invoked by the run() method. Defaults to None, meaning nothing is called.

name is the thread name. By default, a unique name is constructed of the form “Thread-N” where N is a small decimal number.

args is the argument tuple for the target invocation. Defaults to ().

kwargs is a dictionary of keyword arguments for the target invocation. Defaults to {}.

If a subclass overrides the constructor, it must make sure to invoke the base class constructor (Thread.__init__()) before doing anything else to the thread.

addActionOnDemand(task, name)

Add a task to be run only on demand.

Adds a task to the scheduler that will not be scheduled until specifically requested.

addDailyAction(hour, minute, task, name)

Add an action to be run every day at a specific time.

If a task with the given name is already registered with the scheduler, that task will be removed from the scheduling queue and registered anew as a periodic task.

Can we make this addCalendarAction? What if we want to run something once a week? We probably don’t need that for Webware, but this is a more generally useful module. This could be a difficult function, though. Particularly without mxDateTime.

addPeriodicAction(start, period, task, name)

Add a task to be run periodically.

Adds an action to be run at a specific initial time, and every period thereafter.

The scheduler will not reschedule a task until the last scheduled instance of the task has completed.

If a task with the given name is already registered with the scheduler, that task will be removed from the scheduling queue and registered anew as a periodic task.

addTimedAction(actionTime, task, name)

Add a task to be run once, at a specific time.

property daemon

A boolean value indicating whether this thread is a daemon thread.

This must be set before start() is called, otherwise RuntimeError is raised. Its initial value is inherited from the creating thread; the main thread is not a daemon thread and therefore all threads created in the main thread default to daemon = False.

The entire Python program exits when only daemon threads are left.

delOnDemand(name)

Delete a task with the given name from the on demand list.

delRunning(name)

Delete a task from the running list.

Used internally.

delScheduled(name)

Delete a task with the given name from the scheduled list.

demandTask(name)

Demand execution of a task.

Allow the server to request that a task listed as being registered on-demand be run as soon as possible.

If the task is currently running, it will be flagged to run again as soon as the current run completes.

Returns False if the task name could not be found on the on-demand or currently running lists.

disableTask(name)

Specify that a task be suspended.

Suspended tasks will not be scheduled until later enabled. If the task is currently running, it will not be interfered with, but the task will not be scheduled for execution in future until re-enabled.

Returns True if the task was found and disabled.

enableTask(name)

Enable a task again.

This method is provided to specify that a task be re-enabled after a suspension. A re-enabled task will be scheduled for execution according to its original schedule, with any runtimes that would have been issued during the time the task was suspended simply skipped.

Returns True if the task was found and enabled.

getName()
hasOnDemand(name)

Checks whether task with given name is in the on demand list.

hasRunning(name)

Check to see if a task with the given name is currently running.

hasScheduled(name)

Checks whether task with given name is in the scheduled list.

property ident

Thread identifier of this thread or None if it has not been started.

This is a nonzero integer. See the get_ident() function. Thread identifiers may be recycled when a thread exits and another thread is created. The identifier is available even after the thread has exited.

isAlive()

Return whether the thread is alive.

This method is deprecated, use is_alive() instead.

isDaemon()
isRunning()

Check whether thread is running.

is_alive()

Return whether the thread is alive.

This method returns True just before the run() method starts until just after the run() method terminates. The module function enumerate() returns a list of all alive threads.

join(timeout=None)

Wait until the thread terminates.

This blocks the calling thread until the thread whose join() method is called terminates – either normally or through an unhandled exception or until the optional timeout occurs.

When the timeout argument is present and not None, it should be a floating point number specifying a timeout for the operation in seconds (or fractions thereof). As join() always returns None, you must call is_alive() after join() to decide whether a timeout happened – if the thread is still alive, the join() call timed out.

When the timeout argument is not present or None, the operation will block until the thread terminates.

A thread can be join()ed many times.

join() raises a RuntimeError if an attempt is made to join the current thread as that would cause a deadlock. It is also an error to join() a thread before it has been started and attempts to do so raises the same exception.

property name

A string used for identification purposes only.

It has no semantics. Multiple threads may be given the same name. The initial name is set by the constructor.

nextTime()

Get next execution time.

notify()

Wake up scheduler by sending a notify even.

notifyCompletion(handle)

Notify completion of a task.

Used by instances of TaskHandler to let the Scheduler thread know when their tasks have run to completion. This method is responsible for rescheduling the task if it is a periodic task.

notifyFailure(handle)

Notify failure of a task.

Used by instances of TaskHandler to let the Scheduler thread know if an exception has occurred within the task thread.

onDemand(name, default=None)

Return a task from the onDemand list.

onDemandTasks()

Return all on demand tasks.

run()

The main method of the scheduler running as a background thread.

This method is responsible for carrying out the scheduling work of this class on a background thread. The basic logic is to wait until the next action is due to run, move the task from our scheduled list to our running list, and run it. Other synchronized methods such as runTask(), scheduleTask(), and notifyCompletion(), may be called while this method is waiting for something to happen. These methods modify the data structures that run() uses to determine its scheduling needs.

runTask(handle)

Run a task.

Used by the Scheduler thread’s main loop to put a task in the scheduled hash onto the run hash.

runTaskNow(name)

Allow a registered task to be immediately executed.

Returns True if the task is either currently running or was started, or False if the task could not be found in the list of currently registered tasks.

running(name, default=None)

Return running task with given name.

Returns a task with the given name from the “running” list, if it is present there.

runningTasks()

Return all running tasks.

scheduleTask(handle)

Schedule a task.

This method takes a task that needs to be scheduled and adds it to the scheduler. All scheduling additions or changes are handled by this method. This is the only Scheduler method that can notify the run() method that it may need to wake up early to handle a newly registered task.

scheduled(name, default=None)

Return a task from the scheduled list.

scheduledTasks()

Return all scheduled tasks.

setDaemon(daemonic)
setName(name)
setNextTime(nextTime)

Set next execution time.

setOnDemand(handle)

Add the given task to the on demand list.

setRunning(handle)

Add a task to the running dictionary.

Used internally only.

setScheduled(handle)

Add the given task to the scheduled list.

start()

Start the scheduler’s activity.

stop()

Terminate the scheduler and its associated tasks.

stopAllTasks()

Terminate all running tasks.

stopTask(name)

Put an immediate halt to a running background task.

Returns True if the task was either not running, or was running and was told to stop.

unregisterTask(name)

Unregisters the named task.

After that it can be rescheduled with different parameters, or simply removed.

wait(seconds=None)

Our own version of wait().

When called, it waits for the specified number of seconds, or until it is notified that it needs to wake up, through the notify event.