Amp\Parallel\Worker\DefaultPool class DefaultPool implements Amp\Parallel\Worker\Pool

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.

Summary

Properties

Methods

Properties

Methods

__construct — public function __construct(int $maxSize = self::DEFAULT_MAX_SIZE, Amp\Parallel\Worker\WorkerFactory|null $factory = null)

Creates a new worker pool.

Type Parameter Description Default Value
int $maxSize The maximum number of workers the pool should spawn. Defaults to `Pool::DEFAULT_MAX_SIZE`. self::DEFAULT_MAX_SIZE
Amp\Parallel\Worker\WorkerFactory|null $factory A worker factory to be used to create new workers. null
isRunning — public function isRunning(): bool

Checks if the pool is running.

isIdle — public function isIdle(): bool

Checks if the pool has any idle workers.

getMaxSize — public function getMaxSize(): int

Gets the maximum number of workers the pool may spawn to handle concurrent tasks.

getWorkerCount — public function getWorkerCount(): int

Gets the number of workers currently running in the pool.

getIdleWorkerCount — public function getIdleWorkerCount(): int

Gets the number of workers that are currently idle.

enqueue — public function enqueue(Amp\Parallel\Worker\Task $task): Amp\Promise
No documentation.
Type Parameter Description Default Value
Amp\Parallel\Worker\Task $task No documentation. none
shutdown — public function shutdown(): Amp\Promise
No documentation.
kill — public function kill()

Kills all workers in the pool and halts the worker pool.

get — public function get(): Amp\Parallel\Worker\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.

pull — protected function pull(): Amp\Parallel\Worker\Worker

Pulls a worker from the pool. The worker should be put back into the pool with push() to be marked as idle.

push — protected function push(Amp\Parallel\Worker\Worker $worker)

Pushes the worker back into the pool and mark it as idle.

Type Parameter Description Default Value
Amp\Parallel\Worker\Worker $worker No documentation. none