mirror of
https://github.com/danog/parallel.git
synced 2024-11-26 20:34:40 +01:00
Update Pool and DefaultPool
This commit is contained in:
parent
b286c96eb1
commit
d73de0dd5d
@ -17,16 +17,6 @@ use Icicle\Coroutine\Coroutine;
|
||||
*/
|
||||
class DefaultPool implements Pool
|
||||
{
|
||||
/**
|
||||
* @var int The default minimum pool size.
|
||||
*/
|
||||
const DEFAULT_MIN_SIZE = 4;
|
||||
|
||||
/**
|
||||
* @var int The default maximum pool size.
|
||||
*/
|
||||
const DEFAULT_MAX_SIZE = 32;
|
||||
|
||||
/**
|
||||
* @var bool Indicates if the pool is currently running.
|
||||
*/
|
||||
@ -117,9 +107,7 @@ class DefaultPool implements Pool
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the minimum number of workers the pool may have idle.
|
||||
*
|
||||
* @return int The minimum number of workers.
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getMinSize()
|
||||
{
|
||||
@ -127,9 +115,7 @@ class DefaultPool implements Pool
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the maximum number of workers the pool may spawn to handle concurrent tasks.
|
||||
*
|
||||
* @return int The maximum number of workers.
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getMaxSize()
|
||||
{
|
||||
@ -141,7 +127,7 @@ class DefaultPool implements Pool
|
||||
*/
|
||||
public function getWorkerCount()
|
||||
{
|
||||
return count($this->workers);
|
||||
return $this->workers->count();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -253,7 +239,9 @@ class DefaultPool implements Pool
|
||||
$shutdowns = [];
|
||||
|
||||
foreach ($this->workers as $worker) {
|
||||
$shutdowns[] = new Coroutine($worker->shutdown());
|
||||
if ($worker->isRunning()) {
|
||||
$shutdowns[] = new Coroutine($worker->shutdown());
|
||||
}
|
||||
}
|
||||
|
||||
yield Awaitable\reduce($shutdowns, function ($carry, $value) {
|
||||
|
@ -6,6 +6,16 @@ namespace Icicle\Concurrent\Worker;
|
||||
*/
|
||||
interface Pool extends Worker
|
||||
{
|
||||
/**
|
||||
* @var int The default minimum pool size.
|
||||
*/
|
||||
const DEFAULT_MIN_SIZE = 4;
|
||||
|
||||
/**
|
||||
* @var int The default maximum pool size.
|
||||
*/
|
||||
const DEFAULT_MAX_SIZE = 32;
|
||||
|
||||
/**
|
||||
* Gets the number of workers currently running in the pool.
|
||||
*
|
||||
@ -19,4 +29,18 @@ interface Pool extends Worker
|
||||
* @return int The number of idle workers.
|
||||
*/
|
||||
public function getIdleWorkerCount();
|
||||
|
||||
/**
|
||||
* Gets the minimum number of workers the pool may have idle.
|
||||
*
|
||||
* @return int The minimum number of workers.
|
||||
*/
|
||||
public function getMinSize();
|
||||
|
||||
/**
|
||||
* Gets the maximum number of workers the pool may spawn to handle concurrent tasks.
|
||||
*
|
||||
* @return int The maximum number of workers.
|
||||
*/
|
||||
public function getMaxSize();
|
||||
}
|
||||
|
@ -2,14 +2,19 @@
|
||||
namespace Icicle\Tests\Concurrent\Worker;
|
||||
|
||||
use Icicle\Awaitable;
|
||||
use Icicle\Concurrent\Worker\DefaultPool;
|
||||
use Icicle\Coroutine;
|
||||
use Icicle\Loop;
|
||||
use Icicle\Tests\Concurrent\TestCase;
|
||||
|
||||
abstract class AbstractPoolTest extends TestCase
|
||||
{
|
||||
abstract protected function createPool($min = null, $max = null);
|
||||
/**
|
||||
* @param int $min
|
||||
* @param int $max
|
||||
*
|
||||
* @return \Icicle\Concurrent\Worker\Pool
|
||||
*/
|
||||
abstract protected function createPool($min = 0, $max = 0);
|
||||
|
||||
public function testIsRunning()
|
||||
{
|
||||
|
@ -11,7 +11,7 @@ use Icicle\Concurrent\Worker\WorkerFork;
|
||||
*/
|
||||
class ForkPoolTest extends AbstractPoolTest
|
||||
{
|
||||
protected function createPool($min = null, $max = null)
|
||||
protected function createPool($min = 0, $max = 0)
|
||||
{
|
||||
$factory = $this->getMock(WorkerFactory::class);
|
||||
$factory->method('create')->will($this->returnCallback(function () {
|
||||
|
@ -10,7 +10,7 @@ use Icicle\Concurrent\Worker\WorkerProcess;
|
||||
*/
|
||||
class ProcessPoolTest extends AbstractPoolTest
|
||||
{
|
||||
protected function createPool($min = null, $max = null)
|
||||
protected function createPool($min = 0, $max = 0)
|
||||
{
|
||||
$factory = $this->getMock(WorkerFactory::class);
|
||||
$factory->method('create')->will($this->returnCallback(function () {
|
||||
|
@ -11,7 +11,7 @@ use Icicle\Concurrent\Worker\WorkerThread;
|
||||
*/
|
||||
class ThreadPoolTest extends AbstractPoolTest
|
||||
{
|
||||
protected function createPool($min = null, $max = null)
|
||||
protected function createPool($min = 0, $max = 0)
|
||||
{
|
||||
$factory = $this->getMock(WorkerFactory::class);
|
||||
$factory->method('create')->will($this->returnCallback(function () {
|
||||
|
Loading…
Reference in New Issue
Block a user