mirror of
https://github.com/danog/parallel.git
synced 2024-11-26 20:34:40 +01:00
Bring in sample worker API from old branch
This commit is contained in:
parent
bab3c30763
commit
b60f47424a
25
src/Task.php
25
src/Task.php
@ -1,25 +0,0 @@
|
||||
<?php
|
||||
namespace Icicle\Concurrent;
|
||||
|
||||
/**
|
||||
* A synchronous worker object that executes tasks.
|
||||
*/
|
||||
class Task
|
||||
{
|
||||
private $task;
|
||||
|
||||
public function __construct(callable $task)
|
||||
{
|
||||
$this->task = $task;
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs the task inside the caller's context.
|
||||
*
|
||||
* @return [type] [description]
|
||||
*/
|
||||
public function runHere()
|
||||
{
|
||||
call_user_func($this->task);
|
||||
}
|
||||
}
|
15
src/Worker/TaskInterface.php
Normal file
15
src/Worker/TaskInterface.php
Normal file
@ -0,0 +1,15 @@
|
||||
<?php
|
||||
namespace Icicle\Concurrent\Worker;
|
||||
|
||||
/**
|
||||
* A runnable unit of execution.
|
||||
*/
|
||||
interface TaskInterface
|
||||
{
|
||||
/**
|
||||
* Runs the task inside the caller's context.
|
||||
*
|
||||
* Can accept a varied number of arguments sent by the worker.
|
||||
*/
|
||||
public function run();
|
||||
}
|
9
src/Worker/WorkerFactory.php
Normal file
9
src/Worker/WorkerFactory.php
Normal file
@ -0,0 +1,9 @@
|
||||
<?php
|
||||
namespace Icicle\Concurrent\Worker;
|
||||
|
||||
class WorkerFactory
|
||||
{
|
||||
public function create()
|
||||
{
|
||||
}
|
||||
}
|
41
src/Worker/WorkerInterface.php
Normal file
41
src/Worker/WorkerInterface.php
Normal file
@ -0,0 +1,41 @@
|
||||
<?php
|
||||
namespace Icicle\Concurrent\Worker;
|
||||
|
||||
/**
|
||||
* An interface for a parallel worker thread that runs a queue of tasks.
|
||||
*/
|
||||
interface WorkerInterface
|
||||
{
|
||||
/**
|
||||
* Checks if the context is running.
|
||||
*
|
||||
* @return bool True if the context is running, otherwise false.
|
||||
*/
|
||||
public function isRunning();
|
||||
|
||||
/**
|
||||
* Checks if the worker is currently idle.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isIdle();
|
||||
|
||||
/**
|
||||
* Starts the context execution.
|
||||
*/
|
||||
public function start();
|
||||
|
||||
/**
|
||||
* Immediately kills the context.
|
||||
*/
|
||||
public function kill();
|
||||
|
||||
/**
|
||||
* Enqueues a task to be executed by the worker.
|
||||
*
|
||||
* @param TaskInterface $task The task to enqueue.
|
||||
*
|
||||
* @return \Icicle\Promise\PromiseInterface
|
||||
*/
|
||||
public function enqueue(TaskInterface $task);
|
||||
}
|
85
src/Worker/WorkerPool.php
Normal file
85
src/Worker/WorkerPool.php
Normal file
@ -0,0 +1,85 @@
|
||||
<?php
|
||||
namespace Icicle\Concurrent\Worker;
|
||||
|
||||
use Icicle\Concurrent\Exception\InvalidArgumentError;
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
class WorkerPool
|
||||
{
|
||||
/**
|
||||
* @var int The minimum number of workers the pool should spawn.
|
||||
*/
|
||||
private $minSize;
|
||||
|
||||
/**
|
||||
* @var int The maximum number of workers the pool should spawn.
|
||||
*/
|
||||
private $maxSize;
|
||||
|
||||
/**
|
||||
* Creates a new worker pool.
|
||||
*
|
||||
* @param int $minSize The minimum number of workers the pool should spawn.
|
||||
* @param int $maxSize The maximum number of workers the pool should spawn.
|
||||
*/
|
||||
public function __construct(WorkerFactory $factory, $minSize, $maxSize = null)
|
||||
{
|
||||
if (!is_int($minSize) || $minSize < 0) {
|
||||
throw new InvalidArgumentError('Minimum size must be a non-negative integer.');
|
||||
}
|
||||
$this->minSize = $minSize;
|
||||
|
||||
if ($maxSize === null) {
|
||||
$this->maxSize = $minSize;
|
||||
} elseif (!is_int($maxSize) || $maxSize < 0) {
|
||||
throw new InvalidArgumentError('Maximum size must be a non-negative integer.');
|
||||
} else {
|
||||
$this->maxSize = $maxSize;
|
||||
}
|
||||
}
|
||||
|
||||
public function getMinSize()
|
||||
{
|
||||
return $this->minSize;
|
||||
}
|
||||
|
||||
public function getMaxSize()
|
||||
{
|
||||
return $this->maxSize;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the number of workers that have been spawned.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getWorkerCount()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the number of workers that are currently idle.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getIdleWorkerCount()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Enqueues a task to be executed in the worker pool.
|
||||
*
|
||||
* @param TaskInterface $task The task to execute.
|
||||
*
|
||||
* @return \Icicle\Promise\PromiseInterface
|
||||
*/
|
||||
public function enqueue(TaskInterface $task)
|
||||
{
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user