class BackgroundWorkerPool(object):
Constructor: BackgroundWorkerPool(*args, **kwargs)
Pool and factory for background workers and actors.
| Class Method | count |
Return the number of running and paused Jobs. |
| Class Method | find |
Find and return a list of worker(s) that match the search criteria. |
| Class Method | kill |
Stop and remove all Jobs. |
| Class Method | new |
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 | new |
Create a new background worker that periodically executes the callback. |
| Class Method | remove |
Remove workers from the pool. Before removal they will be stopped first. |
| Class Method | run |
Run a task as a Thread. Reuse finished threads if possible. |
| Class Method | set |
Set parameters to balance the number of paused Jobs. |
| Class Method | set |
Assign a callback for logging. |
| Class Method | stop |
Stop the worker(s) that match the optional name parameter. |
| Method | __new__ |
Prevent from instantiation. |
| Class Variable | background |
All background workers. |
| Class Variable | queue |
Lock for the workerQueue. |
| Class Variable | timer |
Lock for the workerTimer. |
| Class Variable | worker |
Priority queue. Contains tuples (next execution timestamp, worker ID, worker name). |
| Class Variable | worker |
A single timer to run the next task in the workerQueue. |
| Class Method | _exec |
Execute the actual BackgroundWorker's callback in a thread. |
| Class Method | _queue |
Queue a BackgroundWorker object for execution at the ts timestamp. |
| Class Method | _remove |
Remove a BackgroundWorker object from the internal pool. |
| Class Method | _start |
Start the workers queue timer. |
| Class Method | _stop |
Cancel/interrupt the workers queue timer. |
| Class Method | _unqueue |
Remove the Backgroundworker for id from the queue. |
Return the number of running and paused Jobs.
| Returns | |
tuple[ | Tuple of the integer numbers (count of running and paused Job instances). |
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 | None | Name of the worker. It may contain simple wildcards (* and ?). If name is None then stop all workers. |
running:bool | None | The running status of the worker to match |
| Returns | |
list[ | A list of BackgroundWorker objects, or an empty list. |
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 | |
workerCallable | Callback 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 | None | Delay 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 | None | Run the actor at a specific time (timestamp). This is an alternative to delay. Only one of at or delay must be specified. |
name:str | None | Name of the actor. |
dispose:bool | None | If True then dispose the actor after finish. |
finished:Callable | None | Callable that is executed after the worker finished. It will receive the same arguments as the workerCallback callback. |
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 in the _data argument of the workerCallback if not None. |
| Returns | |
BackgroundWorker | BackgroundWorker object. It is only an initialized object and needs to be started manually with its start() method. |
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:float | Interval in seconds to run the worker callback |
workerCallable | 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. |
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. |
| Returns | |
BackgroundWorker | BackgroundWorker |
Remove workers from the pool. Before removal they will be stopped first.
Only workers that match the name are removed.
| Parameters | |
name:str | Name of the worker(s) to remove. It may contain simple wildcards (* and ?). |
| Returns | |
list[ | The list of removed BackgroundWorker objects. |
float | None = 3.0, balanceLatency: int | None = 1000, balanceReduceFactor: float | None = 2.0):
¶
Set parameters to balance the number of paused Jobs.
| Parameters | |
balancefloat | None | Target balance between paused and running jobs (n paused for 1 running). |
balanceint | None | Number of requests for getting a new Job before a balance check. |
balancefloat | None | Factor to reduce the paused jobs (number of paused / balanceReduceFactor). |
Stop the worker(s) that match the optional name parameter.
| Parameters | |
name:str | None | Name of the worker(s) to remove. It may contain simple wildcards (* and ?). If name is None then stop all workers. |
| Returns | |
list[ | The list of stopped BackgroundWorker objects. |
Queue a BackgroundWorker object for execution at the ts timestamp.
| Parameters | |
ts:float | Timestamp at which the worker shall be executed. |
worker:BackgroundWorker | Backgroundworker object to queue. |
Remove a BackgroundWorker object from the internal pool.
| Parameters | |
worker:BackgroundWorker | Backgroundworker objects to remove. |
Remove the Backgroundworker for id from the queue.
| Parameters | |
worker:BackgroundWorker | Backgroundworker to unqueue |