apscheduler.schedulers.base
- apscheduler.schedulers.base.STATE_STOPPED = 0
constant indicating a scheduler’s stopped state
- apscheduler.schedulers.base.STATE_RUNNING = 1
constant indicating a scheduler’s running state (started and processing jobs)
- apscheduler.schedulers.base.STATE_PAUSED = 2
constant indicating a scheduler’s paused state (started but not processing jobs)
- class apscheduler.schedulers.base.BaseScheduler(gconfig={}, **options)
Abstract base class for all schedulers.
Takes the following keyword arguments:
- Parameters:
logger (str|logging.Logger) – logger to use for the scheduler’s logging (defaults to apscheduler.scheduler)
timezone (str|datetime.tzinfo) – the default time zone (defaults to the local timezone)
jobstore_retry_interval (int|float) – the minimum number of seconds to wait between retries in the scheduler’s main loop if the job store raises an exception when getting the list of due jobs
job_defaults (dict) – default values for newly added jobs
jobstores (dict) – a dictionary of job store alias -> job store instance or configuration dict
executors (dict) – a dictionary of executor alias -> executor instance or configuration dict
- Variables:
state (int) – current running state of the scheduler (one of the following constants from
apscheduler.schedulers.base
:STATE_STOPPED
,STATE_RUNNING
,STATE_PAUSED
)
See also
- configure(gconfig={}, prefix='apscheduler.', **options)
Reconfigures the scheduler with the given options.
Can only be done when the scheduler isn’t running.
- Parameters:
- Raises:
SchedulerAlreadyRunningError – if the scheduler is already running
- start(paused=False)
Start the configured executors and job stores and begin processing scheduled jobs.
- Parameters:
paused (bool) – if
True
, don’t start job processing untilresume()
is called- Raises:
SchedulerAlreadyRunningError – if the scheduler is already running
RuntimeError – if running under uWSGI with threads disabled
- abstract shutdown(wait=True)
Shuts down the scheduler, along with its executors and job stores.
Does not interrupt any currently running jobs.
- Parameters:
wait (bool) –
True
to wait until all currently executing jobs have finished- Raises:
SchedulerNotRunningError – if the scheduler has not been started yet
- pause()
Pause job processing in the scheduler.
This will prevent the scheduler from waking up to do job processing until
resume()
is called. It will not however stop any already running job processing.
- resume()
Resume job processing in the scheduler.
- property running
Return
True
if the scheduler has been started.This is a shortcut for
scheduler.state != STATE_STOPPED
.
- add_executor(executor, alias='default', **executor_opts)
Adds an executor to this scheduler.
Any extra keyword arguments will be passed to the executor plugin’s constructor, assuming that the first argument is the name of an executor plugin.
- Parameters:
executor (str|unicode|apscheduler.executors.base.BaseExecutor) – either an executor instance or the name of an executor plugin
alias (str|unicode) – alias for the scheduler
- Raises:
ValueError – if there is already an executor by the given alias
- remove_executor(alias, shutdown=True)
Removes the executor by the given alias from this scheduler.
- add_jobstore(jobstore, alias='default', **jobstore_opts)
Adds a job store to this scheduler.
Any extra keyword arguments will be passed to the job store plugin’s constructor, assuming that the first argument is the name of a job store plugin.
- Parameters:
jobstore (str|unicode|apscheduler.jobstores.base.BaseJobStore) – job store to be added
alias (str|unicode) – alias for the job store
- Raises:
ValueError – if there is already a job store by the given alias
- remove_jobstore(alias, shutdown=True)
Removes the job store by the given alias from this scheduler.
- add_listener(callback, mask=EVENT_ALL)
Adds a listener for scheduler events.
When a matching event occurs,
callback
is executed with the event object as its sole argument. If themask
parameter is not provided, the callback will receive events of all types.- Parameters:
callback – any callable that takes one argument
mask (int) – bitmask that indicates which events should be listened to
See also
See also
- remove_listener(callback)
Removes a previously added event listener.
- add_job(func, trigger=None, args=None, kwargs=None, id=None, name=None, misfire_grace_time=undefined, coalesce=undefined, max_instances=undefined, next_run_time=undefined, jobstore='default', executor='default', replace_existing=False, **trigger_args)
Adds the given job to the job list and wakes up the scheduler if it’s already running.
Any option that defaults to
undefined
will be replaced with the corresponding default value when the job is scheduled (which happens when the scheduler is started, or immediately if the scheduler is already running).The
func
argument can be given either as a callable object or a textual reference in thepackage.module:some.object
format, where the first half (separated by:
) is an importable module and the second half is a reference to the callable object, relative to the module.- The
trigger
argument can either be: the alias name of the trigger (e.g.
date
,interval
orcron
), in which case
any extra keyword arguments to this method are passed on to the trigger’s constructor
an instance of a trigger class
- Parameters:
func – callable (or a textual reference to one) to run at the given time
trigger (str|apscheduler.triggers.base.BaseTrigger) – trigger that determines when
func
is calledargs (list|tuple) – list of positional arguments to call func with
kwargs (dict) – dict of keyword arguments to call func with
id (str|unicode) – explicit identifier for the job (for modifying it later)
name (str|unicode) – textual description of the job
misfire_grace_time (int) – seconds after the designated runtime that the job is still allowed to be run (or
None
to allow the job to run no matter how late it is)coalesce (bool) – run once instead of many times if the scheduler determines that the job should be run more than once in succession
max_instances (int) – maximum number of concurrently running instances allowed for this job
next_run_time (datetime) – when to first run the job, regardless of the trigger (pass
None
to add the job as paused)jobstore (str|unicode) – alias of the job store to store the job in
executor (str|unicode) – alias of the executor to run the job with
replace_existing (bool) –
True
to replace an existing job with the sameid
(but retain the number of runs from the existing one)
- Return type:
- The
- scheduled_job(trigger, args=None, kwargs=None, id=None, name=None, misfire_grace_time=undefined, coalesce=undefined, max_instances=undefined, next_run_time=undefined, jobstore='default', executor='default', **trigger_args)
A decorator version of
add_job()
, except thatreplace_existing
is alwaysTrue
.Important
The
id
argument must be given if scheduling a job in a persistent jobstore. The scheduler cannot, however, enforce this requirement.
- modify_job(job_id, jobstore=None, **changes)
Modifies the properties of a single job.
Modifications are passed to this method as extra keyword arguments.
- reschedule_job(job_id, jobstore=None, trigger=None, **trigger_args)
Constructs a new trigger for a job and updates its next run time.
Extra keyword arguments are passed directly to the trigger’s constructor.
- pause_job(job_id, jobstore=None)
Causes the given job not to be executed until it is explicitly resumed.
- resume_job(job_id, jobstore=None)
Resumes the schedule of the given job, or removes the job if its schedule is finished.
- get_jobs(jobstore=None, pending=None)
Returns a list of pending jobs (if the scheduler hasn’t been started yet) and scheduled jobs, either from a specific job store or from all of them.
If the scheduler has not been started yet, only pending jobs can be returned because the job stores haven’t been started yet either.
- get_job(job_id, jobstore=None)
Returns the Job that matches the given
job_id
.
- remove_job(job_id, jobstore=None)
Removes a job, preventing it from being run any more.
- remove_all_jobs(jobstore=None)
Removes all jobs from the specified job store, or all job stores if none is given.
- Parameters:
jobstore (str|unicode) – alias of the job store
- print_jobs(jobstore=None, out=sys.stdout)
Prints out a textual listing of all jobs currently scheduled on either all job stores or just a specific one.
- Parameters:
jobstore (str|unicode) – alias of the job store,
None
to list jobs from all storesout (file) – a file-like object to print to (defaults to sys.stdout if nothing is given)
- abstract wakeup()
Notifies the scheduler that there may be jobs due for execution. Triggers
_process_jobs()
to be run in an implementation specific manner.