1
0
mirror of https://github.com/danog/parallel.git synced 2025-01-22 22:11:11 +01:00

Add standard PoolInterface

This commit is contained in:
coderstephen 2015-08-29 00:41:00 -05:00
parent ef90b760e4
commit 789f6f5620
3 changed files with 37 additions and 18 deletions

View File

@ -15,7 +15,7 @@ use Icicle\Promise\PromiseInterface;
* tasks simultaneously. The load on each worker is balanced such that tasks
* are completed as soon as possible and workers are used efficiently.
*/
class Pool implements WorkerInterface
class Pool implements PoolInterface
{
/**
* @var int The default minimum pool size.
@ -136,9 +136,7 @@ class Pool implements WorkerInterface
}
/**
* Gets the number of workers currently running in the pool.
*
* @return int The number of workers.
* {@inheritdoc}
*/
public function getWorkerCount()
{
@ -146,9 +144,7 @@ class Pool implements WorkerInterface
}
/**
* Gets the number of workers that are currently idle.
*
* @return int The number of idle workers.
* {@inheritdoc}
*/
public function getIdleWorkerCount()
{

View File

@ -0,0 +1,22 @@
<?php
namespace Icicle\Concurrent\Worker;
/**
* An interface for worker pools.
*/
interface PoolInterface extends WorkerInterface
{
/**
* Gets the number of workers currently running in the pool.
*
* @return int The number of workers.
*/
public function getWorkerCount();
/**
* Gets the number of workers that are currently idle.
*
* @return int The number of idle workers.
*/
public function getIdleWorkerCount();
}

View File

@ -7,20 +7,21 @@ if (!function_exists(__NAMESPACE__ . '\pool')) {
/**
* Returns the global worker pool for the current context.
*
* If the pool has not been initialized, a minimum and maximum size can be given to create the pool with.
* @param PoolInterface|null $pool A worker pool instance.
*
* @param int|null $minSize The minimum number of workers the pool should spawn.
* @param int|null $maxSize The maximum number of workers the pool should spawn.
* @param WorkerFactoryInterface|null $factory A worker factory to be used to create new workers.
*
* @return Pool The global worker pool instance.
* @return PoolInterface The global worker pool instance.
*/
function pool($minSize = null, $maxSize = null, WorkerFactoryInterface $factory = null)
function pool(PoolInterface $pool = null)
{
static $instance;
if (null === $instance) {
$instance = new Pool($minSize, $maxSize, $factory);
if (null !== $pool) {
$instance = $pool;
} elseif (null === $instance) {
$instance = new Pool();
}
if (!$instance->isRunning()) {
$instance->start();
}
@ -36,8 +37,8 @@ if (!function_exists(__NAMESPACE__ . '\pool')) {
*
* @resolve mixed The return value of the task.
*/
function enqueue(TaskInterface $task)
function enqueue(TaskInterface $task /* , ...$args */)
{
return new Coroutine(pool()->enqueue($task));
return new Coroutine(call_user_func_array([pool(), 'enqueue'], func_get_args()));
}
}