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
license | MIT
link | http://docblox-project.org

### Methods

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

    __construct(array $input, int $flags, string $iterator_class)

### Parameters

#### $input

`array`

Input for the array object.

#### $flags

`int`

flags for the array object.

#### $iterator_class

`string`

Iterator class for this array object.

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

    addWorker(\DocBlox_Parallel_Worker $worker) : \DocBlox_Parallel_Manager

fluent | This method is part of a fluent interface and will return the same instance

### Parameters

#### $worker

`\DocBlox_Parallel_Worker`

The worker to add onto the queue.

### Returns

`\DocBlox_Parallel_Manager`

## Executes each worker.

    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.

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

    getProcessLimit() : int

### Returns

`int`

## Returns whether the manager is executing the workers.

    isRunning() : boolean

### Returns

`boolean`

## Adds a worker to to the queue.

    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 | \global\DocBlox_Parallel_Manager::execute()

### Parameters

#### $index

`int`

The key for this worker.

#### $newval

`\DocBlox_Parallel_Worker`

The worker to add onto the queue.

### Exceptions

`\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.

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

    setProcessLimit(int $process_limit) : \DocBlox_Parallel_Manager

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

see | for an example
fluent | This method is part of a fluent interface and will return the same instance

### Parameters

#### $process_limit

`int`

The limit, minimum of 1

### Returns

`\DocBlox_Parallel_Manager`

## Returns true when all requirements are met.

    checkRequirements() : bool

### Returns

`bool`

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

    forkAndRun(\DocBlox_Parallel_Worker $worker, array $processes) : void

This is the really tricky part of the forking mechanism. Here we invoke 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 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

#### $worker

`\DocBlox_Parallel_Worker`

The worker to process.

#### $processes

`int[]`

&$processes The list of running processes.

### Exceptions

`\RuntimeException` | if we are unable to fork.

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

    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.

### Returns

`int[]`

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

    stopExecution(array $processes) : void

### Parameters

#### $processes

`int[]`

&$processes List of running processes.

### Properties

## Tracks whether this manager is currently executing

    $is_running : boolean

## The maximum number of processes to run simultaneously

    $process_limit : int