class BackgroundWorker(object):
Constructor: BackgroundWorker(interval, callback, name, startWithDelay, ...)
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 | work |
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 | ignore |
Restart the actor in case an exception is encountered. |
| Instance Variable | interval |
Interval in seconds to run the worker callback. |
| Instance Variable | max |
Maximum number runs. |
| Instance Variable | name |
Name of the worker. |
| Instance Variable | next |
Timestamp of the next execution. |
| Instance Variable | number |
Number of runs. |
| Instance Variable | running |
True if the worker is running. |
| Instance Variable | run |
If True then the worker is always run at the interval, otherwise the interval starts after the worker execution. |
| Instance Variable | run |
If True then missed worker runs in the past are executed, otherwise they are dismissed. |
| Instance Variable | start |
If True then start the worker after a interval delay. |
| Method | _post |
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. |
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:float | Interval in seconds to run the worker callback. |
callback:Callable | Callback to run as a worker. |
name:str | None | Name of the worker. |
startbool | None | If True then start the worker after a interval delay. |
maxint | None | Maximum number runs. |
dispose:bool | None | If True then dispose the worker after finish. |
id:int | None | Unique ID of the worker. |
runbool | None | If True then the worker is always run at the interval, otherwise the interval starts after the worker execution. |
runbool | None | If True then runs in the past are executed, otherwise they are dismissed. |
finished:Callable | None | Callable that is executed after the worker finished. |
ignorebool | None | Restart the actor in case an exception is encountered. |
data:Any | None | 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. |
Restart the worker. Optionally use new interval, and re-use the previous arguments passed with the start() method.
| Parameters | |
interval:float | None | Optional float with the interval. |
| Returns | |
BackgroundWorker | None | The background worker instance, or None if the worker isn't running |
Start the background worker in a thread.
If the background worker is already running then it is stopped and started again.
| Parameters | |
**kwargs:Any | Any number of keyword arguments are passed to the worker. |
| Returns | |
BackgroundWorker | The background worker instance. |
Continue the running of a worker.
| Parameters | |
immediately:bool | None | If True then the worker is executed immediately, and then the normal schedule continues. |
| Returns | |
BackgroundWorker | None | self. |
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.
If True then the worker is always run at the interval, otherwise the interval starts after the worker execution.
If True then missed worker runs in the past are executed, otherwise they are dismissed.