happyslucker / parallel
The Parallel library provides an efficient way to execute multiple tasks concurrently in PHP using child processes. This library leverages the functionality of the pcntl (Process Control) extension to achieve parallel processing, allowing developers to maximize resource utilization and improve perfo
Requires
- php: >=8.1
- ext-pcntl: *
Requires (Dev)
- phpunit/phpunit: ^10.5
This package is not auto-updated.
Last update: 2025-07-09 05:03:45 UTC
README
The Parallel PHP Library provides a simple way to execute multiple functions in parallel processes. This can be useful for improving the performance of applications that perform time-consuming tasks, allowing them to utilize multi-core processors efficiently.
Features
- Run multiple processes in parallel.
- Executes any callable function with specified arguments.
- Communicates between parent and child processes using sockets.
- Easy to integrate into existing PHP applications.
Example 1
function foo($str): string
{
return $str;
}
$parallel = new Parallel();
$data = $parallel(3, true, 'foo', [["Hello World"]])->run();
print_r($data);
/*
Output:
Array
(
[0] => Hello World
[1] => Hello World
[2] => Hello World
)
*/
Example explanation
A Parallel instance and 3 processes are created and each is passed a callback function foo and the same arguments Hello World.
Example 2
function foo($str): string
{
return $str;
}
$parallel = new Parallel();
$data = $parallel(3, true, 'foo', [["Hello World"], ["Test"], ["Hi"]])->run();
print_r($data);
/*
Output:
Array
(
[0] => Hello World
[1] => Test
[2] => Hi
)
*/
Example explanation
A Parallel instance and 3 processes are created and each is passed a callback function foo and the different arguments "Hello World", "Test", "Hi".
Example 3
function foo($str): string
{
return $str;
}
function test(): int
{
return mt_rand(1, 100) + mt_rand(1, 100);
}
$parallel = new Parallel();
$data = $parallel->runProcessorTasks(
[
new ProcessorTask(true, 'test'),
new ProcessorTask(true, 'foo', "test"),
]
);
print_r($data);
/*
Output:
Array
(
[0] => 72
[1] => test
)
*/
Example explanation
In this example we create 2 processes with different functions and different arguments.
Speed comparison
require_once __DIR__ . "/random_calculations.php";
use Parallel\Parallel;
use PHPUnit\Framework\TestCase;
class SpeedTest extends TestCase
{
/**
* @param callable $function
* @param ...$args
* @return float
*/
private function measureExecutionTime(callable $function, ...$args): float
{
$startTime = microtime(true);
$result = $function(...$args);
$endTime = microtime(true);
return $endTime - $startTime;
}
/**
* Test to compare execution time in single-processor and multi-processor
*
* @return void
* @throws \Exception
*/
public function testTimeDifference(): void
{
$coreCount = intval(trim(shell_exec('nproc')));
$multiProcessCallback = function ($processCount, $iterations) {
$parallel = new Parallel();
$parallel($processCount, true, 'foo', [[$iterations]])->run();
};
$singleProcessTime = $this->measureExecutionTime("foo", $coreCount * 1000000);
$multiProcessTime = $this->measureExecutionTime($multiProcessCallback, $coreCount, 1000000);
$this->assertTrue(
$singleProcessTime > $multiProcessTime / $coreCount * 2
);
}
}
/*
* Single-processed execution time: 0.8670539855957031 seconds
* Multi-processed execution time: 0.16166901588439941 seconds
*/
In this test, we can see that using this library we created 12 processes and the execution speed increased more than 5 times.
Important Notes
The library uses the pcntl_fork() function, which is only available on Unix-like operating systems (Linux, macOS). This library will not work on Windows. Make sure to handle exceptions and errors in your callbacks appropriately, as unhandled exceptions can cause your child processes to terminate unexpectedly.
Conclusion
This Parallel library makes it possible to leverage the power of parallel processing in PHP in a clean and structured manner.