class documentation

Job class that extends the Thread class with pause, resume, stop functionalities, and lists of running and paused jobs for reuse.

Job objects are not deleted immediately after they finished but pooled for reuse. They are only destroyed when the pressure on the pool was low for a certain time.

Class Method getJob Get a Job object, and set a task and a finished Callable for it to execute. The Job object is either taken from the paused list (if available), or a new one is created. After calling this method the Job instance is neither in the paused nor the running list...
Class Method setJobBalance Set parameters to balance the number of paused Jobs.
Method __init__ Initialize a Job object.
Method pause Pause a thread job. The job is removed from the running list (if still present there) and moved to the paused list.
Method resume Resume a thread job. The job is removed from the paused list (if still present there) and moved to the running list.
Method run Internal runner function for a job.
Method setTask Set a task to run for the Job.
Method stop Stop a thread job
Class Variable __slots__ Slots for the class.
Class Variable jobListLock Lock for the job lists.
Class Variable pausedJobs List of paused jobs.
Class Variable runningJobs List of running jobs.
Instance Variable activeFlag Indicates that a job is active. An active job might be paused.
Instance Variable finished Optional callback that is called after the task finished.
Instance Variable name Name of the job.
Instance Variable pauseFlag The flag used to pause the thread
Instance Variable task Callback for the job's task.
Class Method _balanceJobs Internal function to balance the number of paused and running jobs.
Class Variable _balanceCount Counter for current runs. Compares against balance.
Class Variable _balanceLatency Number of requests for getting a new Job before a balance check.
Class Variable _balanceReduceFactor Factor to reduce the paused jobs (number of paused / balanceReduceFactor).
Class Variable _balanceTarget Target balance between paused and running jobs (n paused for 1 running).
@classmethod
def getJob(cls, task: Callable, finished: Callable | None = None, name: str | None = None) -> Job:

Get a Job object, and set a task and a finished Callable for it to execute. The Job object is either taken from the paused list (if available), or a new one is created. After calling this method the Job instance is neither in the paused nor the running list. It is moved into the running list, for example, with the resume() method.

Parameters
task:CallableA Callable. This must include arguments, so a lambda can be used here.
finished:Callable | NoneA Callable that is called when the task finished.
name:str | NoneOptional name of the job.
Returns
JobThe Job object.
@classmethod
def setJobBalance(cls, balanceTarget: float | None = 3.0, balanceLatency: int | None = 1000, balanceReduceFactor: float | None = 2.0):

Set parameters to balance the number of paused Jobs.

Parameters
balanceTarget:float | NoneTarget balance between paused and running jobs (n paused for 1 running).
balanceLatency:int | NoneNumber of requests for getting a new Job before a balance check.
balanceReduceFactor:float | NoneFactor to reduce the paused jobs (number of paused / balanceReduceFactor).
def __init__(self, *args: Any, **kwargs: Any):

Initialize a Job object.

Parameters
*args:AnyPositional job arguments.
**kwargs:AnyKeyword job arguments.
def pause(self) -> Job:

Pause a thread job. The job is removed from the running list (if still present there) and moved to the paused list.

Returns
JobThe Job object.
def resume(self) -> Job:

Resume a thread job. The job is removed from the paused list (if still present there) and moved to the running list.

Returns
JobThe Job object.
def run(self):

Internal runner function for a job.

def setTask(self, task: Callable, finished: Callable | None = None, name: str | None = None) -> Job:

Set a task to run for the Job.

Parameters
task:CallableA Callable. This must include arguments, so a lambda can be used here.
finished:Callable | NoneA Callable that is called when the task finished.
name:str | NoneOptional name of the job.
Returns
JobThe Job object.
def stop(self) -> Job:

Stop a thread job

Returns
JobThe Job object.
__slots__: tuple[str, ...] =

Slots for the class.

jobListLock =

Lock for the job lists.

pausedJobs: list[Job] =

List of paused jobs.

runningJobs: list[Job] =

List of running jobs.

activeFlag =

Indicates that a job is active. An active job might be paused.

finished: Callable =

Optional callback that is called after the task finished.

name: str | None =

Name of the job.

pauseFlag =

The flag used to pause the thread

task: Callable =

Callback for the job's task.

@classmethod
def _balanceJobs(cls):

Internal function to balance the number of paused and running jobs.

_balanceCount: int =

Counter for current runs. Compares against balance.

_balanceLatency: int =

Number of requests for getting a new Job before a balance check.

_balanceReduceFactor: float =

Factor to reduce the paused jobs (number of paused / balanceReduceFactor).

_balanceTarget: float =

Target balance between paused and running jobs (n paused for 1 running).