lib/Pagon/ChildProcess.php
public function parallel($closure, $options = array(), $start = true)
    {
        // Check auto_start
        if (is_bool($options)) {
            $start = $options;
            $options = array();
        }

        // Check if process set
        if ($closure instanceof Process) {
            $child = $closure;
            $closure = $child->runner;
            $options = $child->options;
        } else {
            // Build new child
            $child = new Process($this, null, $this->pid);

            // Get options
            $options = $this->getOptions($options);
        }

        // Auto start
        if (!$start) {
            $child->register($closure, $options);
            if (!in_array($child, $this->prepared_children)) {
                $this->prepared_children[] = $child;
            }
            return $child;
        }

        // Process init
        if ($options['init'] instanceof \Closure) {
            $options['init']($child);
        }

        // Fork
        $pid = pcntl_fork();

        // Save child process and return
        $this->children[$pid] = $child;
        // Remove from prepared children
        if (($index = array_search($child, $this->prepared_children)) !== false) {
            unset($this->prepared_children[$index]);
        }

        // Parallel works
        if ($pid === -1) {
            throw new \RuntimeException('Unable to fork child process.');
        } else if ($pid) {
            // Save child process and return
            $child->init($pid);
            $child->emit('fork');
            return $child;
        } else {
            // Child initialize
            $this->childInitialize($options);

            // Support callable
            call_user_func($closure, $this->process);
            exit;
        }
    }
public function parallel($closure, $options = array(), $start = true)
    {
        // Check auto_start
        if (is_bool($options)) {
            $start = $options;
            $options = array();
        }

        // Check if process set
        if ($closure instanceof Process) {
            $child = $closure;
            $closure = $child->runner;
            $options = $child->options;
        } else {
            // Build new child
            $child = new Process($this, null, $this->pid);

            // Get options
            $options = $this->getOptions($options);
        }

        // Auto start
        if (!$start) {
            $child->register($closure, $options);
            if (!in_array($child, $this->prepared_children)) {
                $this->prepared_children[] = $child;
            }
            return $child;
        }

        // Process init
        if ($options['init'] instanceof \Closure) {
            $options['init']($child);
        }

        // Fork
        $pid = pcntl_fork();

        // Save child process and return
        $this->children[$pid] = $child;
        // Remove from prepared children
        if (($index = array_search($child, $this->prepared_children)) !== false) {
            unset($this->prepared_children[$index]);
        }

        // Parallel works
        if ($pid === -1) {
            throw new \RuntimeException('Unable to fork child process.');
        } else if ($pid) {
            // Save child process and return
            $child->init($pid);
            $child->emit('fork');
            return $child;
        } else {
            // Child initialize
            $this->childInitialize($options);

            // Support callable
            call_user_func($closure, $this->process);
            exit;
        }
    }
public function parallel ( $ closure , $ options = array ( ) , $ start = true ) { // Check auto_start if ( is_bool ( $ options ) ) { $ start = $ options ; $ options = array ( ) ; } // Check if process set if ( $ closure instanceof Process ) { $ child = $ closure ; $ closure = $ child -> runner ; $ options = $ child -> options ; } else { // Build new child $ child = new Process ( $ this , null , $ this -> pid ) ; // Get options $ options = $ this -> getOptions ( $ options ) ; } // Auto start if ( ! $ start ) { $ child -> register ( $ closure , $ options ) ; if ( ! in_array ( $ child , $ this -> prepared_children ) ) { $ this -> prepared_children [ ] = $ child ; } return $ child ; } // Process init if ( $ options [ 'init' ] instanceof \ Closure ) { $ options [ 'init' ] ( $ child ) ; } // Fork $ pid = pcntl_fork ( ) ; // Save child process and return $ this -> children [ $ pid ] = $ child ; // Remove from prepared children if ( ( $ index = array_search ( $ child , $ this -> prepared_children ) ) !== false ) { unset ( $ this -> prepared_children [ $ index ] ) ; } // Parallel works if ( $ pid === - 1 ) { throw new \ RuntimeException ( 'Unable to fork child process.' ) ; } else if ( $ pid ) { // Save child process and return $ child -> init ( $ pid ) ; $ child -> emit ( 'fork' ) ; return $ child ; } else { // Child initialize $ this -> childInitialize ( $ options ) ; // Support callable call_user_func ( $ closure , $ this -> process ) ; exit ; } }
Run the closure in parallel space

@param callable|Process $closure
@param array|\Closure   $options
@param bool             $start
@throws \RuntimeException
@return Process
Run the closure in parallel space