class documentation

This class provides the functionality for background worker or a single actor instance.

Background workers are executed in a separate thread.

They are executed periodically according to the interval. The interval is the time between the end of the previous execution and the start of the next execution. The interval is usually not the time betweenthe start of two consecutive executions, but this could be achieved by setting the runOnTime parameter to True. This will compensate for the processing time of the worker callback.

Background workers can be stopped and started again. They can also be paused and resumed.

Method __init__ Initialize a background worker.
Method __repr__ Return a string representation of the worker.
Method pause Pause the execution of a a worker.
Method restart Restart the worker. Optionally use new interval, and re-use the previous arguments passed with the start() method.
Method start Start the background worker in a thread.
Method stop Stop the background worker.
Method unpause Continue the running of a worker.
Method workNow Execute the worker immediately and outside the normal schedule.
Class Variable __slots__ Slots for the class.
Instance Variable args Arguments for the callback.
Instance Variable callback Callback function to run as a worker.
Instance Variable data Any 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.
Instance Variable dispose If True then dispose the worker after finish.
Instance Variable executing True if the worker is currently executing.
Instance Variable finished Callback that is executed after the worker finished.
Instance Variable id Unique ID of the worker.
Instance Variable ignoreException Restart the actor in case an exception is encountered.
Instance Variable interval Interval in seconds to run the worker callback.
Instance Variable maxCount Maximum number runs.
Instance Variable name Name of the worker.
Instance Variable nextRunTime Timestamp of the next execution.
Instance Variable numberOfRuns Number of runs.
Instance Variable running True if the worker is running.
Instance Variable runOnTime If True then the worker is always run at the interval, otherwise the interval starts after the worker execution.
Instance Variable runPastEvents If True then missed worker runs in the past are executed, otherwise they are dismissed.
Instance Variable startWithDelay If True then start the worker after a interval delay.
Method _postCall Internal cleanup after execution finished or a worker has been stopped.
Method _work Wrapper around the actual worker function. It deals with terminating, process time compensation, etc.
def __init__(self, interval: float, callback: Callable, name: str | None = None, startWithDelay: bool | None = False, maxCount: int | None = None, dispose: bool | None = True, id: int | None = None, runOnTime: bool | None = True, runPastEvents: bool | None = False, finished: Callable | None = None, ignoreException: bool | None = False, data: Any | None = None):

Initialize a background worker.

Parameters
interval:floatInterval in seconds to run the worker callback.
callback: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.
id:int | NoneUnique ID of the worker.
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.
def __repr__(self) -> str:

Return a string representation of the worker.

Returns
strA string representation of the worker.
def pause(self) -> BackgroundWorker:

Pause the execution of a a worker.

Returns
BackgroundWorkerThe background worker instance.
def restart(self, interval: float | None = None) -> BackgroundWorker | None:

Restart the worker. Optionally use new interval, and re-use the previous arguments passed with the start() method.

Parameters
interval:float | NoneOptional float with the interval.
Returns
BackgroundWorker | NoneThe background worker instance, or None if the worker isn't running
def start(self, **kwargs: Any) -> BackgroundWorker:

Start the background worker in a thread.

If the background worker is already running then it is stopped and started again.

Parameters
**kwargs:AnyAny number of keyword arguments are passed to the worker.
Returns
BackgroundWorkerThe background worker instance.
def stop(self) -> BackgroundWorker:

Stop the background worker.

Returns
BackgroundWorkerThe background worker instance.
def unpause(self, immediately: bool | None = False) -> BackgroundWorker | None:

Continue the running of a worker.

Parameters
immediately:bool | NoneIf True then the worker is executed immediately, and then the normal schedule continues.
Returns
BackgroundWorker | Noneself.
def workNow(self) -> BackgroundWorker:

Execute the worker immediately and outside the normal schedule.

Returns
BackgroundWorkerself.
__slots__: tuple[str, ...] =

Slots for the class.

args: dict[str, Any] =

Arguments for the callback.

callback =

Callback function to run as a worker.

data =

Any 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.

dispose =

If True then dispose the worker after finish.

executing: bool =

True if the worker is currently executing.

finished =

Callback that is executed after the worker finished.

id =

Unique ID of the worker.

ignoreException =

Restart the actor in case an exception is encountered.

interval =

Interval in seconds to run the worker callback.

maxCount =

Maximum number runs.

name =

Name of the worker.

nextRunTime: float =

Timestamp of the next execution.

numberOfRuns: int =

Number of runs.

running: bool =

True if the worker is running.

runOnTime =

If True then the worker is always run at the interval, otherwise the interval starts after the worker execution.

runPastEvents =

If True then missed worker runs in the past are executed, otherwise they are dismissed.

startWithDelay =

If True then start the worker after a interval delay.

def _postCall(self):

Internal cleanup after execution finished or a worker has been stopped.

def _work(self):

Wrapper around the actual worker function. It deals with terminating, process time compensation, etc.

This wrapper and the callback are executed in a separate Thread. At the end, depending on return value and whether the maxCount has been reached, the worker is added to the queue again.