1
0
mirror of https://github.com/danog/parallel.git synced 2024-12-12 09:09:35 +01:00
parallel/lib/Worker/Internal/PooledWorker.php

74 lines
1.4 KiB
PHP
Raw Normal View History

2016-12-30 02:16:04 +01:00
<?php
2016-08-23 23:47:40 +02:00
namespace Amp\Parallel\Worker\Internal;
2017-05-18 09:51:31 +02:00
use Amp\Parallel\Worker\Task;
use Amp\Parallel\Worker\Worker;
use Amp\Promise;
2016-08-18 18:04:48 +02:00
class PooledWorker implements Worker {
2016-08-26 17:10:03 +02:00
/** @var callable */
private $push;
2016-08-26 17:10:03 +02:00
/** @var \Amp\Parallel\Worker\Worker */
private $worker;
/**
2016-08-23 23:47:40 +02:00
* @param \Amp\Parallel\Worker\Worker $worker
* @param callable $push Callable to push the worker back into the queue.
*/
2016-08-18 18:04:48 +02:00
public function __construct(Worker $worker, callable $push) {
$this->worker = $worker;
$this->push = $push;
}
/**
* Automatically pushes the worker back into the queue.
*/
2016-08-18 18:04:48 +02:00
public function __destruct() {
2016-01-23 07:00:56 +01:00
($this->push)($this->worker);
}
/**
* {@inheritdoc}
*/
2016-08-18 18:04:48 +02:00
public function isRunning(): bool {
return $this->worker->isRunning();
}
/**
* {@inheritdoc}
*/
2016-08-18 18:04:48 +02:00
public function isIdle(): bool {
return $this->worker->isIdle();
}
/**
* {@inheritdoc}
*/
2016-08-18 18:04:48 +02:00
public function start() {
$this->worker->start();
}
/**
* {@inheritdoc}
*/
2016-11-15 00:43:44 +01:00
public function enqueue(Task $task): Promise {
return $this->worker->enqueue($task);
}
/**
* {@inheritdoc}
*/
2016-11-15 00:43:44 +01:00
public function shutdown(): Promise {
return $this->worker->shutdown();
}
/**
* {@inheritdoc}
*/
2016-08-18 18:04:48 +02:00
public function kill() {
$this->worker->kill();
}
2017-05-18 09:51:31 +02:00
}