PHPackages                             rikanishu/php-multi-process - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. rikanishu/php-multi-process

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

rikanishu/php-multi-process
===========================

PHP library for parallel shell commands execution

0.2.4(9y ago)97.3k[1 issues](https://github.com/Rikanishu/php-multi-process/issues)BSD-3-ClausePHPPHP &gt;=5.4.0

Since Dec 6Pushed 8y ago2 watchersCompare

[ Source](https://github.com/Rikanishu/php-multi-process)[ Packagist](https://packagist.org/packages/rikanishu/php-multi-process)[ RSS](/packages/rikanishu-php-multi-process/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (5)Dependencies (1)Versions (6)Used By (0)

php-multi-process
=================

[](#php-multi-process)

PHP library for parallel shell command execution [![Build Status](https://camo.githubusercontent.com/41f5cdf3ab9a47b26ffb05018e1a8e2e8c4d3ea30bf75cbaa044d023478288b1/68747470733a2f2f7472617669732d63692e6f72672f52696b616e697368752f7068702d6d756c74692d70726f636573732e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/Rikanishu/php-multi-process)
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

[](#php-library-for-parallel-shell-command-execution)

### Installation via composer:

[](#installation-via-composer)

Either run

```
php composer.phar require rikanishu/php-multi-process "*"

```

or add

```
"rikanishu/php-multi-process": "*"

```

to the `require` section of your `composer.json` file.

### Example:

[](#example)

```
$cmd1 = ['echo', '"Some Command"'];
$cmd2 = 'echo "Another Command"';
$cmd3 = ['echo "$SOME_ENV_VAR" "$PWD"', [
    rikanishu\multiprocess\Command::OPTION_CWD => '/tmp',
    rikanishu\multiprocess\Command::OPTION_ENV =>  [
        'SOME_ENV_VAR' => 'PWD is:'
    ],
]];
$cmd4 = new rikanishu\multiprocess\Command('cat');
$cmd4->setStdin('hello world');

$pool = new rikanishu\multiprocess\Pool([$cmd1, $cmd2, $cmd3, $cmd4]);
$pool->run();

/* @var $command rikanishu\multiprocess\Command */
foreach ($pool->getCommands() as $command) {
    $res = $command->getExecutionResult();
    echo $res->getExitCode() . " | " . $res->getStdout() . " | " . $res->getStderr() . "\n";
}

/*  Output:
    0 | Some Command |
    0 | Another Command |
    0 | PWD is: /tmp |
*/

$commands = $pool->getCommands();
$commands[0]->getExecutionResult()->getOutput(); // Some Command
$commands[1]->getExecutionResult()->getOutput(); // Another Command
$commands[2]->getExecutionResult()->getOutput(); // PWD is: /tmp
$commands

/* And also library provides single command execution */
$command = new rikanishu\multiprocess\Command('echo "Hello World!"');
$command->runBlocking()->getOutput(); // Hello World
```

### Description

[](#description)

This library is designed to execute single / multiple parallel process on blocking / non-blocking mode. The library provides an interface for convenient configuring external process execution. The basic concepts are Pool and Command objects. Pool is a set of commands that creates and delegates execution to ExecutionContext, ExecutionContext builds Process objects for each command and runs it. Command object represents single external command.

The library uses proc\_\* API and select sys call that are provided by standard PHP library. It has no external dependency.

#### Usage

[](#usage)

General usage scenario suggests that you pass some commands to a Pool instance which will be executed parallel on blocking / non-blocking mode depends on params passed on. Here is example of non-blocking execution of four commands:

```
$cmd1 = ['echo', '"Some Command"'];
$cmd2 = 'echo "Another Command"';
$cmd3 = ['echo "$SOME_ENV_VAR" "$PWD"', [
    rikanishu\multiprocess\Command::OPTION_CWD => '/tmp',
    rikanishu\multiprocess\Command::OPTION_ENV =>  [
        'SOME_ENV_VAR' => 'PWD is:'
    ],
]];
$cmd4 = new rikanishu\multiprocess\Command('cat');
$cmd4->setStdin('hello world');

$pool = new rikanishu\multiprocess\Pool([$cmd1, $cmd2, $cmd3, $cmd4]);
$pool->run();

/* @var $command rikanishu\multiprocess\Command */
foreach ($pool->getCommands() as $command) {
    $res = $command->getExecutionResult();
    echo $res->getExitCode() . " | " . $res->getStdout() . " | " . $res->getStderr() . "\n";
}
```

`$cmd1`, `$cmd2`, `$cmd3`, and `$cmd4` show different format of commands applied by Pool. You can also pass instance of Command right away because Pool implicitly converts shell command text to Command instances and checks input data for this class first.

```
…
$cmd5 = new rikanishu\multiprocess\Command(‘echo instance’);
…

$pool = new rikanishu\multiprocess\Pool([$cmd1, $cmd2, $cmd3, $cmd4, $cmd5]);
$pool->run();
```

The Pool’s `run()` method returns Futures array that represents each command, e.g. `$future[3]` represents `$cmd4` from example above. You can always use `$command->getFuture()` method to get current future for executed command. Non executed command does not have Future and if you call `getFuture()` for non-executed Command, Exception will be raised. You can call `hasFuture()` for checking when your code does not know, has command been ran or hasn't.

```
$cmd1 = ['echo', '"Some Command"'];
$cmd2 = 'echo "Another Command"';
$cmd3 = new rikanishu\multiprocess\Command(‘echo instance’);

$pool = new rikanishu\multiprocess\Pool([$cmd1, $cmd2, $cmd3]);
$futures = $pool->run();
$commands = $pool->getCommands();

print_r(count($commands)); // Count of command objects always equals passed shell commands. Pool raise an exception on creation step if command has invalid format.
print_r($cmd3 == $commands[2]); // Equals
print_r($commands[1]->hasFuture()); // True, command is running
print_r($commands[1]->getFuture() == $futures[1]); // Equals
print_r($commands[1]->getFuture()->getResult()); // Block and waiting execution result
//Or you can block process directly by alias command method
print_r($command[1]->getExecutionResult()); // Alias of Future’s getResult()
```

On non blocking mode `$pool->run()` call creates a process array and returns control to calling procedure. The process will execute at the meantime in background and when you call Future’s `getResult()` method, it blocks execution process until result is received or time limit is expired. You can always check Future’s `hasResult()` if you want to avoid blocking and continue your useful calculating process.

```
$pool = new rikanishu\multiprocess\Pool([$cmd1, $cmd2, $cmd3]);
$futures = $pool->run();
…
//doing some useful work
…
if ($furures[1]->hasResult()) {
…
}
//doing another useful work
…
//block and waiting data finally
$result = $futures[1]->getResult();
```

And also if you need to run only single command you can use command method `run()` directly. It will create new Pool with single command and return Future. Use method `runBlocking()` to run execution in blocking mode and receive execution result.

```
$command = new rikanishu\multiprocess\Command('echo "Hello World!"');
$command->runBlocking()->getOutput(); // Hello World
```

Or

```
$command = new rikanishu\multiprocess\Command('echo "Hello World!"');
$future = $command->run();
…
// doing some useful work
…
$future->getResult()->getOutput(); // Hello World
```

#### Pool options

[](#pool-options)

Pool takes options array as second parameter. You can set some options to Pool by two ways:

- Pass an array of options as second param to Pool construct:

```
$pool = new rikanishu\multiprocess\Pool([$cmd1, $cmd2, $cmd3], [
	rikanishu\multiprocess\Pool::OPTION_EXECUTION_TIMEOUT => 120,
	rikanishu\multiprocess\Pool::OPTION_BLOCKING_MODE => true
]);
```

- Set param after the object creating:

```
$pool->setExecutionTimeout(120);
$pool->setBlockingMode(true);
```

##### Options list

[](#options-list)

- `OPTION_EXECUTION_TIMEOUT` - Limit of time in seconds for execution process. Values are less than zero interpret as unlimited time out. Default is -1 (has no limits). Alias method for option is `$pool->setExecutionTimeout(200)`.
- `OPTION_POLL_TIMEOUT` - Maximum timeout in seconds for every select poll cycle. This means time for waiting any react of running process and if it has no any react (out text to stdout / stderr or stop execution for example), retry the poll cycle after reading process status. Default value is 60 seconds. Alias method for option is `$pool->setPollTimeout(120)`.
- `OPTION_SELECT_USLEEP_TIME` - Time for poll cycle in microseconds if select call returns false or does not supported by system for proc polling (for example on Windows). Default is 200 microseconds. Alias method for option is `$pool->setSelectUsleepTime(400)`.
- `OPTION_DEBUG` - Debug mode. Enables some output debug messages for execution. Default is false. Alas for this method is `$pool->setDebugEnabled(true)`
- `OPTION_BLOCKING_MODE` - Blocking mode flag. If blocking mode used, process will be stopped for all execution time. In other case, current process can interact with Future to wait results or check execution status. Default is false, non-blocking mode. Alias for this method is `$pool->setBlockingMode(true)`.

#### Command options

[](#command-options)

You can configure Command same as Pool by two ways:

```
$command = new rikanishu\multiprocess\Command(‘echo ‘Hello $NAME’’, [
	rikanishu\multiprocess\Command::OPTIONS_ENV => [
		‘NAME’ => ‘WORLD’,
	],
	rikanishu\multiprocess\Command::OPTIONS_CWD => ‘/tmp’
]);
```

Or

```
$command->setEnvVariables([
	‘NAME’ => ‘WORLD’,
]);
$command->setCwdPath(‘/tmp’);
```

And also you can control options when you pass shell command to Pool construct as array second element:

```
$cmd1 = ‘echo Hello!’;
$cmd2 = ['echo "$SOME_ENV_VAR" "$PWD"', [
    rikanishu\multiprocess\Command::OPTION_CWD => '/tmp',
    rikanishu\multiprocess\Command::OPTION_ENV =>  [
        'SOME_ENV_VAR' => 'PWD is:'
    ],
]];

$pool = new rikanishu\multiprocess\Pool([$cmd1, $cmd2]);
$pool->run();
```

##### Options list

[](#options-list-1)

- `OPTION_ENV` - Environment variables list for command. Default is null. Alias method for this option is `$command->setEnvVariables([])`.
- `OPTION_CWD` - Current working directory for command. Default is null and CWD inherits from parent process. Alias method for this option is `$command->setCwdPath(‘/tmp’)`.
- `OPTION_PROC` - Options for proc\_open command. See full list in [PHP Documentation](http://php.net/manual/en/function.proc-open.php). Alias method for this option is `$command->setProcOptions([])`.
- `OPTION_STDIN` - Stdin for command. Alias method for this option is `$command->setStdin('')`.
- `OPTION_DONT_CHECK_RUNNING` - Don't wait the end of execution and don't check process state to recognize whether it runs or doesn't, just read stdin/stdout and stop process. Useful in working with pipes to stop execution process after it gives any output. Alias method for this option is `$command->setDontCheckRunning(true)`.

###  Health Score

29

—

LowBetter than 59% of packages

Maintenance18

Infrequent updates — may be unmaintained

Popularity26

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~161 days

Total

5

Last Release

3534d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/986d250ce91176f9ae20563c5f2b5601d61bee702ff0e0604accc738fd061f88?d=identicon)[Rikanishu](/maintainers/Rikanishu)

---

Top Contributors

[![Rikanishu](https://avatars.githubusercontent.com/u/894419?v=4)](https://github.com/Rikanishu "Rikanishu (31 commits)")

---

Tags

multitaskingphpshellprocesstaskmulti-processparallel execution

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/rikanishu-php-multi-process/health.svg)

```
[![Health](https://phpackages.com/badges/rikanishu-php-multi-process/health.svg)](https://phpackages.com/packages/rikanishu-php-multi-process)
```

###  Alternatives

[phing/phing

PHing Is Not GNU make; it's a PHP project build system or build tool based on Apache Ant.

1.2k21.7M876](/packages/phing-phing)[react/child-process

Event-driven library for executing child processes with ReactPHP.

34076.1M136](/packages/react-child-process)[cocur/background-process

Start processes in the background that continue running when the PHP process exists.

2971.9M12](/packages/cocur-background-process)[arara/process

Provides a better API to work with processes on Unix-like systems

16861.7k2](/packages/arara-process)[phpmentors/workflower

A BPMN 2.0 workflow engine for PHP

70652.9k4](/packages/phpmentors-workflower)[graze/supervisor

:vertical\_traffic\_light: Process supervisor for PHP.

999.3k](/packages/graze-supervisor)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
