API Reference

pyramid_cron.add_cron_task(config, f, min='*', hour='*', day='*', month='*', dow='*', idle=False)[source]

Register a function for execution by the scheduler.

Task functions must have the following signature:

def mytask(system):
    request = system['request']
    registry = system['registry']
    time_left = system['time_left']
    # do stuff

Additional keys may be added in the future: the single-arg signature ensures that task functions will be forward-compatible.

The time_left member is a no-parameter function that returns how many seconds are remaining in the allotted 60 seconds of the current cron run. When the 60 seconds is exceeded, the returned value will be negative.

In addition to the callback function, you can specify a schedule, using a cron-like syntax. For the time periods of min, hour, day, month, and dow (day of week), you can specify an integer, a set of integers, or the ‘*’ wildcard character. The default argument is ‘*’. Hours are specified in 24-hour time.

For example, this will run the task every day, at 2:00:

config.add_cron_task(..., hour=2)

This will run the task every day at 2:00, 10:00, and 18:00:

config.add_cron_task(..., hour=[2, 10, 18])

To run the task ‘every 2 hours’, you can use range():

config.add_cron_task(..., hour=range(0, 24, 2))
Parameters:
  • f – The function to execute. Task functions must have accept a single argument, which will be a system dict containing keys for the Pyramid request and registry.
  • min – Specify which minutes to run the task.
  • hour – Specify which hours to run the task.
  • day – Specify which days to run the task.
  • month – Specify which months to run the task.
  • dow – Specify which days of the week to run the task.
  • idle – If true, executes the task after all non-idle tasks have completed, and only when there is time remaining in the 60 second window since the cron view was triggered.