ContextWorkerPool
in package
implements
LimitedWorkerPool
uses
ForbidCloning, ForbidSerialization
Provides a pool of workers that can be used to execute multiple tasks asynchronously.
A worker pool is a collection of worker threads that can perform multiple tasks simultaneously. The load on each worker is balanced such that tasks are completed as soon as possible and workers are used efficiently.
Table of Contents
Interfaces
- LimitedWorkerPool
- An interface for worker pools.
Properties
- $deferredCancellation : DeferredCancellation
- $exitStatus : Future|null
- $factory : WorkerFactory|null
- $idleWorkers : SplQueue<string|int, Worker>
- $limit : int
- $pendingWorkerCount : int
- $push : callable(Worker): void
- $waiting : SplQueue<string|int, DeferredFuture<string|int, Worker|null>>
- $workers : SplObjectStorage<Worker, int>
Methods
- __construct() : mixed
- Creates a new worker pool.
- __destruct() : mixed
- __serialize() : never
- __unserialize() : never
- getIdleWorkerCount() : int
- Gets the number of workers that are currently idle.
- getLimit() : int
- Gets the maximum number of workers the pool may spawn to handle concurrent tasks.
- getWorker() : Worker
- Gets a worker from the pool. The worker is marked as busy and will only be reused if the pool runs out of idle workers. The worker will be automatically marked as idle once no references to the returned worker remain.
- getWorkerCount() : int
- Gets the number of workers currently running in the pool.
- getWorkerLimit() : int
- Gets the maximum number of workers the pool may spawn to handle concurrent tasks.
- isIdle() : bool
- Checks if the pool has any idle workers.
- isRunning() : bool
- Checks if the pool is running.
- kill() : void
- Kills all workers in the pool and halts the worker pool.
- shutdown() : void
- Shuts down the pool and all workers in it.
- submit() : Execution<TReceive, TSend>
- Submits a {@see Task} to be executed by the worker pool.
- __clone() : mixed
- killWorkers() : void
- pull() : Worker
- Pulls a worker from the pool.
Properties
$deferredCancellation read-only
private
DeferredCancellation
$deferredCancellation
$exitStatus
private
Future|null
$exitStatus
= null
$factory read-only
private
WorkerFactory|null
$factory
= null
$idleWorkers read-only
private
SplQueue<string|int, Worker>
$idleWorkers
A collection of idle workers.
$limit read-only
private
int
$limit
= self::DEFAULT_WORKER_LIMIT
$pendingWorkerCount
private
int
$pendingWorkerCount
= 0
$push read-only
private
callable(Worker): void
$push
$waiting read-only
private
SplQueue<string|int, DeferredFuture<string|int, Worker|null>>
$waiting
Task submissions awaiting an available worker.
$workers read-only
private
SplObjectStorage<Worker, int>
$workers
A collection of all workers in the pool.
Methods
__construct()
Creates a new worker pool.
public
__construct([int $limit = self::DEFAULT_WORKER_LIMIT ][, WorkerFactory|null $factory = null ]) : mixed
Parameters
- $limit : int = self::DEFAULT_WORKER_LIMIT
-
The maximum number of workers the pool should spawn. Defaults to
Pool::DEFAULT_MAX_SIZE. - $factory : WorkerFactory|null = null
-
A worker factory to be used to create new workers.
Tags
__destruct()
public
__destruct() : mixed
__serialize()
public
final __serialize() : never
Return values
never__unserialize()
public
final __unserialize(array<string|int, mixed> $data) : never
Parameters
- $data : array<string|int, mixed>
Return values
nevergetIdleWorkerCount()
Gets the number of workers that are currently idle.
public
getIdleWorkerCount() : int
Return values
int —The number of idle workers.
getLimit()
Gets the maximum number of workers the pool may spawn to handle concurrent tasks.
public
getLimit() : int
Use getWorkerLimit() instead.
Return values
int —The maximum number of workers.
getWorker()
Gets a worker from the pool. The worker is marked as busy and will only be reused if the pool runs out of idle workers. The worker will be automatically marked as idle once no references to the returned worker remain.
public
getWorker() : Worker
This method does not guarantee the worker will be dedicated to a particular task, rather is designed if you wish to send a series of tasks to a single worker. For a dedicated worker, create a new worker using a WorkerFactory or createWorker().
Return values
WorkergetWorkerCount()
Gets the number of workers currently running in the pool.
public
getWorkerCount() : int
Return values
int —The number of workers.
getWorkerLimit()
Gets the maximum number of workers the pool may spawn to handle concurrent tasks.
public
getWorkerLimit() : int
Return values
int —The maximum number of workers.
isIdle()
Checks if the pool has any idle workers.
public
isIdle() : bool
Return values
bool —True if the pool has at least one idle worker, otherwise false.
isRunning()
Checks if the pool is running.
public
isRunning() : bool
Return values
bool —True if the pool is running, otherwise false.
kill()
Kills all workers in the pool and halts the worker pool.
public
kill() : void
shutdown()
Shuts down the pool and all workers in it.
public
shutdown() : void
Tags
submit()
Submits a {@see Task} to be executed by the worker pool.
public
submit(Task $task[, Cancellation|null $cancellation = null ]) : Execution<TReceive, TSend>
Parameters
- $task : Task
-
The task to execute.
- $cancellation : Cancellation|null = null
-
Token to request cancellation. The task must support cancellation for this to have any effect.
Return values
Execution<TReceive, TSend>__clone()
protected
final __clone() : mixed
killWorkers()
private
static killWorkers(SplObjectStorage<Worker, int> $workers, SplQueue<string|int, DeferredFuture<string|int, Worker|null>> $waiting[, Throwable|null $exception = null ]) : void
Parameters
- $workers : SplObjectStorage<Worker, int>
- $waiting : SplQueue<string|int, DeferredFuture<string|int, Worker|null>>
- $exception : Throwable|null = null
pull()
Pulls a worker from the pool.
private
pull() : Worker