job — Asynchronous job manager

The JobManager provides a way to schedule jobs and run tasks asynchronously from within python on the local system. In this case jobs are callback functions defined by the user.

Warning

The timing and scheduling functions within this module are not designed to be precise to the second.

Functions

normalize_job_id(job_id)[source]

Convert a value to a job id.

Parameters:job_id (int, str) – Value to convert.
Returns:The job id.
Return type:uuid.UUID

Classes

class JobManager(use_utc=True, logger_name=None)[source]

This class provides a threaded job manager for periodically executing arbitrary functions in an asynchronous fashion.

__init__(use_utc=True, logger_name=None)[source]
Parameters:
  • use_utc (bool) – Whether or not to use UTC time internally.
  • logger_name (str) – A specific name to use for the logger.
job_add(callback, parameters=None, hours=0, minutes=0, seconds=0, tolerate_exceptions=True, expiration=None)[source]

Add a job to the job manager.

Parameters:
  • callback (function) – The function to run asynchronously.
  • parameters (list, tuple) – The parameters to be provided to the callback.
  • hours (int) – Number of hours to sleep between running the callback.
  • minutes (int) – Number of minutes to sleep between running the callback.
  • seconds (int) – Number of seconds to sleep between running the callback.
  • tolerate_execptions (bool) – Whether to continue running a job after it has thrown an exception.
  • expiration (int, datetime.timedelta, datetime.datetime) – When to expire and remove the job. If an integer is provided, the job will be executed that many times. If a datetime or timedelta instance is provided, then the job will be removed after the specified time.
Returns:

The job id.

Return type:

uuid.UUID

job_count()[source]

Return the number of jobs.

Returns:The number of jobs.
Return type:int
job_count_enabled()[source]

Return the number of enabled jobs.

Returns:The number of jobs that are enabled.
Return type:int
job_delete(job_id, wait=True)[source]

Delete a job.

Parameters:
  • job_id (uuid.UUID) – Job identifier to delete.
  • wait (bool) – If the job is currently running, wait for it to complete before deleting it.
job_disable(job_id)[source]

Disable a job. Disabled jobs will not be executed.

Parameters:job_id (uuid.UUID) – Job identifier to disable.
job_enable(job_id)[source]

Enable a job.

Parameters:job_id (uuid.UUID) – Job identifier to enable.
job_exists(job_id)[source]

Check if a job identifier exists.

Parameters:job_id (uuid.UUID) – Job identifier to check.
Return type:bool
job_is_enabled(job_id)[source]

Check if a job is enabled.

Parameters:job_id (uuid.UUID) – Job identifier to check the status of.
Return type:bool
job_is_running(job_id)[source]

Check if a job is currently running. False is returned if the job does not exist.

Parameters:job_id (uuid.UUID) – Job identifier to check the status of.
Return type:bool
job_run(callback, parameters=None)[source]

Add a job and run it once immediately.

Parameters:
  • callback (function) – The function to run asynchronously.
  • parameters (list, tuple) – The parameters to be provided to the callback.
Returns:

The job id.

Return type:

uuid.UUID

now()[source]

Return a datetime.datetime instance representing the current time.

Return type:datetime.datetime
now_is_after(dt)[source]

Check whether the datetime instance described in dt is after the current time.

Parameters:dt (datetime.datetime) – Value to compare.
Return type:bool
now_is_before(dt)[source]

Check whether the datetime instance described in dt is before the current time.

Parameters:dt (datetime.datetime) – Value to compare.
Return type:bool
start()[source]

Start the JobManager thread.

stop()[source]

Stop the JobManager thread.

class JobRequestDelete[source]

An instance of this class can be returned by a job callback to request that the job be deleted and not executed again.