Manager class for Parallel processes.
This class will manage the workers and make sure all processes are executed in parallel and not too many at the same time.
category |
DocBlox |
---|---|
package |
Parallel |
author |
Mike van Riel mike.vanriel@naenius.com |
license |
http://www.opensource.org/licenses/mit-license.php MIT |
link |
http://docblox-project.org |
__construct(array $input = array(), int $flags, string $iterator_class = "ArrayIterator")
array
Input for the array object.
int
flags for the array object.
string
Iterator class for this array object.
offsetSet(int $index, \DocBlox_Parallel_Worker $newval) : void
This method will prepare a worker to be executed in parallel once the execute method is invoked. A fluent interface is provided so that you can chain multiple workers in one call.
Example:
$cb1 = function() { var_dump('a'); sleep(1); }; $cb2 = function() { var_dump('b'); sleep(1); };
$mgr = new DocBlox_Parallel_Manager(); $mgr->setProcessLimit(2) ->addWorker(new DocBlox_Parallel_Worker($cb1)) ->addWorker(new DocBlox_Parallel_Worker($cb2)) ->execute();
see | \DocBlox_Parallel_Manager::execute() |
---|---|
throws |
if this method is invoked while the manager is busy executing tasks. if the provided element is not of type DocBlox_Parallel_Worker. |
int
The key for this worker.
addWorker(\DocBlox_Parallel_Worker $worker) : self
setProcessLimit(int $process_limit) : self
A fluent interface is provided so that you can chain multiple workers in one call.
see |
\DocBlox_Parallel_Manager::addWorker()
for an example |
---|
int
The limit, minimum of 1
self
getProcessLimit() : int
int
isRunning() : boolean
boolean
execute() : void
This method loops through the list of workers and tries to fork as many times as the ProcessLimit dictates at the same time.
startExecution() : int[]
If forking is not available because library requirements are not met than the list of workers is processed in series and a E_USER_NOTICE is triggered.
int[]
stopExecution(array $processes) : void
array
forkAndRun(\DocBlox_Parallel_Worker $worker, array $processes) : void
This is the really tricky part of the forking mechanism. Here we invoke {@link http://www.php.net/manual/en/function.pcntl-fork.php pcntl_fork} and either execute the forked process or deal with the parent's process based on in which process we are.
To fully understand what is going on here it is recommended to read the PHP manual page on {@link http://www.php.net/manual/en/function.pcntl-fork.php pcntl_fork} and associated articles.
If there are more workers than may be ran simultaneously then this method will wait until a slot becomes available and then starts the next worker.
throws |
if we are unable to fork. |
---|
array
checkRequirements() : bool
bool
process_limit : int
var |
The maximum number of processes to run simultaneously |
---|
int
is_running : boolean
var |
Tracks whether this manager is currently executing |
---|
boolean