Re: Boost performance with parallel processing

Martin Roest posted an article about parallel processing. Here’s a more reusable version of the code, OOP style:

Usage with callback function:

<?php
$pc = new ProcessController(10);
$pc->process($documents, 'processDocument');
?>

It supports static method callbacks:

$pc->process($documents, array('DocumentProcessor', 'processDocument'));

And callbacks on an instance of an object:

$pc->process($documents, array($documentProcessor, 'processDocument'));

Class:

<?php
 
class ProcessController
{
    protected $maxNumChildren = 1;
 
    public function __construct($maxNumChildren = 1)
    {
        $this->setMaxNumChildren($maxNumChildren);
    }
 
    public function getMaxNumChildren()
    {
        return $this->maxNumChildren;
    }
 
    public function setMaxNumChildren($maxNumChildren)
    {
        $this->maxNumChildren = $maxNumChildren;
    }
 
    public function process($iterator, $callback)
    {
        $maxNumChildren = $this->getMaxNumChildren();
        $children = array();
 
        foreach ($iterator as $item)
        {
            $pid = pcntl_fork();
 
            if ($pid == -1)
            {
                // unable to fork
                return false;
            }
            else if ($pid)
            {
                // parent process
                $children[] = $pid;
                if (count($children) >= $maxNumChildren)
                {
                    $pid = array_shift($children);
                    pcntl_waitpid($pid, $status);
                }
            }
            else
            {
                // child process
                call_user_func($callback, $item);
                exit(0);
            }
        }
 
        // still in parent process, make sure all children are finished
        foreach ($children as $pid)
        {
            pcntl_waitpid($pid, $status);
        }
 
        return true;
    }
}
 
?>

Tags: , ,

Comments are closed.