contrib/DocBlox/Parallel/Manager.php

Show: inherited
Table of Contents

DocBlox

PHP Version 5

Author
Mike van Riel  
Category
DocBlox  
Copyright
2010-2011 Mike van Riel / Naenius (http://www.naenius.com)  
License
MIT  
Link
http://docblox-project.org  
Package
Parallel  

\DocBlox_Parallel_Manager

Package: Parallel

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.

Parent(s)
\ArrayObject
Author
Mike van Riel  
Category
DocBlox  
License
MIT  
Link
http://docblox-project.org  

Properties

Propertyprotectedboolean  $is_running= 'false'

Tracks whether this manager is currently executing
Default valuefalseDetails
Type
boolean
Propertyprotectedint  $process_limit= '2'

The maximum number of processes to run simultaneously
Default value2Details
Type
int

Methods

methodpublic__construct( array $input = array(), int $flags = 0, string $iterator_class = "ArrayIterator" ) : void

Tries to autodetect the optimal number of process by counting the number of processors.

Parameters
Name Type Description
$input array

Input for the array object.

$flags int

flags for the array object.

$iterator_class string

Iterator class for this array object.

methodpublicaddWorker( \DocBlox_Parallel_Worker $worker ) : \DocBlox_Parallel_Manager

Convenience method to make the addition of workers explicit and allow a fluent interface.

Parameters
Name Type Description
$worker \DocBlox_Parallel_Worker

The worker to add onto the queue.

Returns
Type Description
\DocBlox_Parallel_Manager
Details
Fluent
This method is part of a fluent interface and will return the same instance  
methodpublicappend( ) : void

methodpublicasort( ) : void

methodprotectedcheckRequirements( ) : bool

Returns true when all requirements are met.

Returns
Type Description
bool
methodpubliccount( ) : void

methodpublicexchangeArray( ) : void

methodpublicexecute( ) : void

Executes each worker.

This method loops through the list of workers and tries to fork as many times as the ProcessLimit dictates at the same time.

methodprotectedforkAndRun( \DocBlox_Parallel_Worker $worker, \int[] $processes ) : void

Forks the current process and calls the Worker's execute method OR handles the parent process' execution.

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.

Parameters
Name Type Description
$worker \DocBlox_Parallel_Worker

The worker to process.

$processes \int[]

&$processes The list of running processes.

Throws
Exception Description
\RuntimeException if we are unable to fork.
methodpublicgetArrayCopy( ) : void

methodpublicgetFlags( ) : void

methodpublicgetIterator( ) : void

methodpublicgetIteratorClass( ) : void

methodpublicgetProcessLimit( ) : int

Returns the current limit on the amount of processes that can be executed at the same time.

Returns
Type Description
int
methodpublicisRunning( ) : boolean

Returns whether the manager is executing the workers.

Returns
Type Description
boolean
methodpublicksort( ) : void

methodpublicnatcasesort( ) : void

methodpublicnatsort( ) : void

methodpublicoffsetExists( ) : void

methodpublicoffsetGet( ) : void

methodpublicoffsetSet( int $index, \DocBlox_Parallel_Worker $newval ) : void

Adds a worker to to the queue.

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();

Parameters
Name Type Description
$index int

The key for this worker.

$newval \DocBlox_Parallel_Worker

The worker to add onto the queue.

Throws
Exception Description
\RuntimeException if this method is invoked while the manager is busy executing tasks.
\InvalidArgumentException if the provided element is not of type DocBlox_Parallel_Worker.
Details
See
\DocBlox_Parallel_Manager::execute()  
methodpublicoffsetUnset( ) : void

methodpublicserialize( ) : void

methodpublicsetFlags( ) : void

methodpublicsetIteratorClass( ) : void

methodpublicsetProcessLimit( int $process_limit ) : \DocBlox_Parallel_Manager

Sets how many processes at most to execute at the same time.

A fluent interface is provided so that you can chain multiple workers in one call.

Parameters
Name Type Description
$process_limit int

The limit, minimum of 1

Returns
Type Description
\DocBlox_Parallel_Manager
Details
Fluent
This method is part of a fluent interface and will return the same instance  
See
\DocBlox_Parallel_Manager::addWorker()  
methodprotectedstartExecution( ) : \int[]

Notifies manager that execution has started, checks requirements and returns array for child processes.

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.

Returns
Type Description
\int[]
methodprotectedstopExecution( \int[] $processes ) : void

Waits for all processes to have finished and notifies the manager that execution has stopped.

Parameters
Name Type Description
$processes \int[]

&$processes List of running processes.

methodpublicuasort( ) : void

methodpublicuksort( ) : void

methodpublicunserialize( ) : void

Documentation was generated by DocBlox 0.18.1.