Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save EngKhaledB/3f57dde16731c09d9429b2f6a51fe5c4 to your computer and use it in GitHub Desktop.
Save EngKhaledB/3f57dde16731c09d9429b2f6a51fe5c4 to your computer and use it in GitHub Desktop.
SIMPLE PARALLEL PROCESSING IN PHP WITH PCNTL_FORK
<?php
# Source: https://jrsinclair.com/articles/2013/simple-parallel-processing-in-php-with-pcntl-fork/
// A function to run
$makeDir = function($a) {
shell_exec('mkdir '.shellescapearg($a));
}
// An array to process
$dirnames = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k');
// Run the process in parallel.
processParallel($makeDir, $dirnames, 8);
/**
* Process in Parallel.
*
* Run a function (with no return result) on each item in an array in parallel.
* Note: This function is only useful if order is not important, and you don't
* need any return values from the function (i.e. no inter-process communication).
*
* @param mixed $func A closure function to apply to each item in parallel.
* @param array $arr The array to apply function to.
* @param integer $procs Number of processes to run in parallel.
*
* @return void
*/
function processParallel($func, array $arr, $procs=4)
{
// Break array up into $procs chunks.
$chunks = array_chunk($arr, ceil((count($arr) / $procs)));
$pid = -1;
$children = array();
foreach ($chunks as $items) {
$pid = pcntl_fork();
if ($pid === -1) {
die('could not fork');
} else if ($pid === 0) {
// We are the child process. Pass a chunk of items to process.
array_walk($items, $func);
exit(0);
} else {
// We are the parent.
$children[] = $pid;
}
}
// Wait for children to finish.
foreach ($children as $pid) {
// We are still the parent.
pcntl_waitpid($pid, $status);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment