I just thought of contributing to this awesome community and hope this can be of use to someone. Although PHP provides threaded options, and multi curl handles that run in parallel, I managed to bash out a solution to run each function as it's own process for non-threaded versions of PHP.
Usage: #!/usr/bin/php
Usage: php -f /path/to/file
#!/usr/bin/php
<?php
function fork_process($options)
{
$shared_memory_monitor = shmop_open(ftok(__FILE__, chr(0)), "c", 0644, count($options['process']));
$shared_memory_ids = (object) array();
for ($i = 1; $i <= count($options['process']); $i++)
{
$shared_memory_ids->$i = shmop_open(ftok(__FILE__, chr($i)), "c", 0644, $options['size']);
}
for ($i = 1; $i <= count($options['process']); $i++)
{
$pid = pcntl_fork();
if (!$pid)
{
if($i==1)
usleep(100000);
$shared_memory_data = $options['process'][$i - 1]();
shmop_write($shared_memory_ids->$i, $shared_memory_data, 0);
shmop_write($shared_memory_monitor, "1", $i-1);
exit($i);
}
}
while (pcntl_waitpid(0, $status) != -1)
{
if(shmop_read($shared_memory_monitor, 0, count($options['process'])) == str_repeat("1", count($options['process'])))
{
$result = array();
foreach($shared_memory_ids as $key=>$value)
{
$result[$key-1] = shmop_read($shared_memory_ids->$key, 0, $options['size']);
shmop_delete($shared_memory_ids->$key);
}
shmop_delete($shared_memory_monitor);
$options['callback']($result);
}
}
}
$options['size'] = pow(1024,2);
$options['process'][0] = function()
{
sleep(1);
return 'Hello ';
};
$options['process'][1] = function()
{
sleep(1);
return 'World!';
};
$options['callback'] = function($result)
{
echo $result[0].$result[1]."\n";
};
fork_process($options);
?>
If you'd like to get the results back from a webpage, use exec(). Eg: echo exec('php -f /path/to/file');
Continue hacking! :)