Worker

Handle your jobs on the worker pool thread.

Work Interface

 /**
  * @param callable $work
  * @return void
  *
  * The work is a function that will be executed when the work is run.
  */
 public function add(callable $work): void;

 /**
  * @param int $index
  * @return void
  *
  * Remove the work from the work list.
  */
 public function remove(int $index): void;

 /**
  * @return void
  *
  * Remove all works from the work list.
  */
 public function clear(): void;

 /**
  * @return int
  *
  * Get the number of works in the work list.
  */
 public function count(): int;

 /**
  * @return bool
  *
  * Check if the work list is empty.
  */
 public function isEmpty(): bool;

 /**
  * @return mixed
  *
  * Get the first work in the work list.
  */
 public function dequeue(): mixed;

 /**
  * @param int $number
  * @return Generator
  *
  * Get the work list by number.
  */
 public function getArrayByNumber(int $number): Generator;

 /**
  * @return Generator
  *
  * Get all works in the work list.
  */
 public function getAll(): Generator;

 /**
  * @return void
  *
  * Run all works in the work list.
  */
 public function run(): void;

Worker Interface

 /**
  * @return bool
  *
  * Check the worker is started.
  */
 public function isStarted(): bool;

 /**
  * @return Work
  *
  * Get the work.
  */
 public function getWork(): Work;

 /**
  * @return void
  *
  * This is method help you to remove the worker from the worker list.
  * You should call this method when the work is done to avoid memory leaks.
  */
 public function done(): void;

 /**
  * @param mixed $result
  * @return void
  *
  * Collect the result of the work.
  */
 public function collect(mixed $result): void;

 /**
  * @return array<int|string, mixed>
  *
  * Get the result of the work.
  */
 public function get(): array;

 /**
  * @return bool
  *
  * Check the worker is locked.
  */
 public function isLocked(): bool;

 /**
  * @return void
  *
  * Lock the worker.
  */
 public function lock(): void;

 /**
  * @return void
  *
  * Unlock the worker.
  */
 public function unlock(): void;

 /**
  * @param Worker $worker
  * @param callable $callback
  * @return void
  *
  * Add a child worker to the parent worker.
  */
 public function addWorker(Worker $worker, callable $callback): void;

 /**
  * @return Async
  *
  * Run the work.
  * @throws Throwable
  */
 public function run(callable $callback): Async;

Last updated