class documentation

class BackgroundWorkerPool(object):

Constructor: BackgroundWorkerPool(*args, **kwargs)

View In Hierarchy

Pool and factory for background workers and actors.

Class Method countJobs Return the number of running and paused Jobs.
Class Method findWorkers Find and return a list of worker(s) that match the search criteria.
Class Method killJobs Stop and remove all Jobs.
Class Method newActor Create a new background worker that runs only once after a delay (it may be 0.0s, though), or at a specific time (UTC timestamp).
Class Method newWorker Create a new background worker that periodically executes the callback.
Class Method removeWorkers Remove workers from the pool. Before removal they will be stopped first.
Class Method runJob Run a task as a Thread. Reuse finished threads if possible.
Class Method setJobBalance Set parameters to balance the number of paused Jobs.
Class Method setLogger Assign a callback for logging.
Class Method stopWorkers Stop the worker(s) that match the optional name parameter.
Method __new__ Prevent from instantiation.
Class Variable backgroundWorkers All background workers.
Class Variable queueLock Lock for the workerQueue.
Class Variable timerLock Lock for the workerTimer.
Class Variable workerQueue Priority queue. Contains tuples (next execution timestamp, worker ID, worker name).
Class Variable workerTimer A single timer to run the next task in the workerQueue.
Class Method _execQueue Execute the actual BackgroundWorker's callback in a thread.
Class Method _queueWorker Queue a BackgroundWorker object for execution at the ts timestamp.
Class Method _removeBackgroundWorkerFromPool Remove a BackgroundWorker object from the internal pool.
Class Method _startTimer Start the workers queue timer.
Class Method _stopTimer Cancel/interrupt the workers queue timer.
Class Method _unqueueWorker Remove the Backgroundworker for id from the queue.
@classmethod
def countJobs(cls) -> tuple[int, int]:

Return the number of running and paused Jobs.

Returns
tuple[int, int]Tuple of the integer numbers (count of running and paused Job instances).
@classmethod
def findWorkers(cls, name: str | None = None, running: bool | None = None) -> list[BackgroundWorker]:

Find and return a list of worker(s) that match the search criteria.

Parameters
name:str | NoneName of the worker. It may contain simple wildcards (* and ?). If name is None then stop all workers.
running:bool | NoneThe running status of the worker to match
Returns
list[BackgroundWorker]A list of BackgroundWorker objects, or an empty list.
@classmethod
def killJobs(cls):

Stop and remove all Jobs.

@classmethod
def newActor(cls, workerCallback: Callable, delay: float | None = 0.0, at: float | None = None, name: str | None = None, dispose: bool | None = True, finished: Callable | None = None, ignoreException: bool | None = False, data: Any | None = None) -> BackgroundWorker:

Create a new background worker that runs only once after a delay (it may be 0.0s, though), or at a specific time (UTC timestamp).

Parameters
workerCallback:CallableCallback that is executed to perform the action for the actor. It will receive the data in its _data, and the worker itself in the _worker arguments (if available as arguments).
delay:float | NoneDelay in seconds after which the actor callback is executed. This is an alternative to at. Only one of at or delay must be specified.
at:float | NoneRun the actor at a specific time (timestamp). This is an alternative to delay. Only one of at or delay must be specified.
name:str | NoneName of the actor.
dispose:bool | NoneIf True then dispose the actor after finish.
finished:Callable | NoneCallable that is executed after the worker finished. It will receive the same arguments as the workerCallback callback.
ignoreException:bool | NoneRestart the actor in case an exception is encountered.
data:Any | NoneAny data structure that is stored in the worker and accessible by the data attribute, and which is passed in the _data argument of the workerCallback if not None.
Returns
BackgroundWorkerBackgroundWorker object. It is only an initialized object and needs to be started manually with its start() method.
@classmethod
def newWorker(cls, interval: float, workerCallback: Callable, name: str | None = None, startWithDelay: bool | None = False, maxCount: int | None = None, dispose: bool | None = True, runOnTime: bool | None = True, runPastEvents: bool | None = False, finished: Callable | None = None, ignoreException: bool | None = False, data: Any | None = None) -> BackgroundWorker:

Create a new background worker that periodically executes the callback.

Parameters
interval:floatInterval in seconds to run the worker callback
workerCallback:CallableCallback to run as a worker
name:str | NoneName of the worker
startWithDelay:bool | NoneIf True then start the worker after a interval delay
maxCount:int | NoneMaximum number runs
dispose:bool | NoneIf True then dispose the worker after finish.
runOnTime:bool | NoneIf True then the worker is always run at the interval, otherwise the interval starts after the worker execution.
runPastEvents:bool | NoneIf True then runs in the past are executed, otherwise they are dismissed.
finished:Callable | NoneCallable that is executed after the worker finished.
ignoreException:bool | NoneRestart the actor in case an exception is encountered.
data:Any | NoneAny data structure that is stored in the worker and accessible by the data attribute, and which is passed as the first argument in the _data argument of the workerCallback if not None.
Returns
BackgroundWorkerBackgroundWorker
@classmethod
def removeWorkers(cls, name: str) -> list[BackgroundWorker]:

Remove workers from the pool. Before removal they will be stopped first.

Only workers that match the name are removed.

Parameters
name:strName of the worker(s) to remove. It may contain simple wildcards (* and ?).
Returns
list[BackgroundWorker]The list of removed BackgroundWorker objects.
@classmethod
def runJob(cls, task: Callable, name: str | None = None) -> Job:

Run a task as a Thread. Reuse finished threads if possible.

Parameters
task:CallableA Callable that is run as a job. This must include arguments, so a lambda can be used here.
name:str | NoneOptional name of the job.
Returns
JobJob instance.
@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).
@classmethod
def setLogger(cls, logger: Callable):

Assign a callback for logging.

Parameters
logger:CallableLogging callback.
@classmethod
def stopWorkers(cls, name: str | None = None) -> list[BackgroundWorker]:

Stop the worker(s) that match the optional name parameter.

Parameters
name:str | NoneName of the worker(s) to remove. It may contain simple wildcards (* and ?). If name is None then stop all workers.
Returns
list[BackgroundWorker]The list of stopped BackgroundWorker objects.
def __new__(cls, *args: str, **kwargs: str) -> BackgroundWorkerPool:

Prevent from instantiation.

This class has static functions only.

backgroundWorkers: dict[int, BackgroundWorker] =

All background workers.

queueLock: Lock =

Lock for the workerQueue.

timerLock: Lock =

Lock for the workerTimer.

workerQueue: list[WorkerEntry] =

Priority queue. Contains tuples (next execution timestamp, worker ID, worker name).

workerTimer: Timer =

A single timer to run the next task in the workerQueue.

@classmethod
def _execQueue(cls):

Execute the actual BackgroundWorker's callback in a thread.

@classmethod
def _queueWorker(cls, ts: float, worker: BackgroundWorker):

Queue a BackgroundWorker object for execution at the ts timestamp.

Parameters
ts:floatTimestamp at which the worker shall be executed.
worker:BackgroundWorkerBackgroundworker object to queue.
@classmethod
def _removeBackgroundWorkerFromPool(cls, worker: BackgroundWorker):

Remove a BackgroundWorker object from the internal pool.

Parameters
worker:BackgroundWorkerBackgroundworker objects to remove.
@classmethod
def _startTimer(cls):

Start the workers queue timer.

@classmethod
def _stopTimer(cls):

Cancel/interrupt the workers queue timer.

@classmethod
def _unqueueWorker(cls, worker: BackgroundWorker):

Remove the Backgroundworker for id from the queue.

Parameters
worker:BackgroundWorkerBackgroundworker to unqueue