Attachs a new process to the pool.

Try to detach finished processes from the pool when reaches its limit.

@param Process $process
185861
public function attach(Process $process)
    {
        if ($this->stopped) {
            throw new RuntimeException('Could not attach child to non-running pool');
        }

        $firstProcess = $this->getFirstProcess();
        if ($this->isRunning()
            && $firstProcess instanceof Process
            && $this->count() >= $this->processLimit) {
            $firstProcess->wait();
            $this->detach($firstProcess);
        }
        $this->process->attach($process);

        if ($this->isRunning()) {
            $process->start();
        }
    }
423745
public function process()
    {
        if ($this->isProcessed()) {
            throw new MultiProcessException('Already Processed!');
        }

        if ($this->doProcess()) {
            $this->setProcessed(true);
        }

        return $this;
    }
722557
public function finalize()
    {
        /** @var Process $process */
        $process = $this->process;

        if ($process) {
            $process->wait();

            if ($process->isSuccessful()) {
                $this->successful = true;
                $this->output =  $process->getOutput();
            } else {
                $this->successful = false;
                $this->output =  $process->getErrorOutput();
            }

            $this->postRun();

            unset($this->process);
        }
    }
932341
private function runProcess(Process $process)
    {
        $process->setTimeout(null)->mustRun();
        foreach (explode(PHP_EOL, $process->getOutput()) as $line) {
            $this->notice($line);
        }
    }
334674
public function add($process): Runnable
    {
        if (! is_callable($process) && ! $process instanceof Runnable) {
            throw new InvalidArgumentException('The process passed to Pool::add should be callable.');
        }

        if (! $process instanceof Runnable) {
            $process = ParentRuntime::createProcess($process);
        }

        $this->putInQueue($process);

        return $process;
    }
775336
protected function attach($stream)
    {
        if (is_resource($stream) === false) {
            throw new InvalidArgumentException(__METHOD__ . ' argument must be a valid PHP resource');
        }

        if (is_resource($this->stream) === true) {
            $this->detach();
        }

        $this->stream = $stream;
    }
1242062
def set_process(self, process = None):
        
        if process is None:
            self.dwProcessId = None
            self.__process   = None
        else:
            self.__load_Process_class()
            if not isinstance(process, Process):
                msg  = "Parent process must be a Process instance, "
                msg += "got %s instead" % type(process)
                raise TypeError(msg)
            self.dwProcessId = process.get_pid()
            self.__process = process
937874
public function removeProcessByName($name)
    {
        foreach ($this->processes as $key => $process) {
            if ($process->name() == $name) {
                if ($process->isRunning()) {
                    throw new \RuntimeException("can not remove a running process");
                }
                unset($this->processes[$key]);
            }
        }
    }
1084771
public function join()
    {
        while (true) {
            $finished = true;
            foreach ($this->processes as $pid => $process) {
                if ($process->isAlive()) {
                    $finished = false;
                    break;
                }
            }

            if ($finished) {
                break;
            }

            unset($finished);
            usleep(10000);
        }
    }
576647
private function runCommand(Process $process)
    {
        $output = $this->output;

        /** @type DebugFormatterHelper $debugFormatter */
        $debugFormatter = $this->getHelperSet()->get('debug_formatter');
        if (null !== $dispatcher = $this->getEventDispatcher()) {
            $event = new PreExecuteEvent($this, $process);
            $dispatcher->dispatch(Event::PRE_EXECUTE, $event);

            if ($event->isPropagationStopped()) {
                $output->writeln(
                    ['', sprintf('    <info>[%s]</info> - <comment>Stopped</comment>', $this->getName()), '']
                );

                return true;
            }
        }

        if ($output->getVerbosity() === OutputInterface::VERBOSITY_VERBOSE || $this->getParameter('dry_run')) {
            $output->writeln('        // '.$process->getCommandLine());
        }

        if ($this->getParameter('dry_run')) {
            return true;
        }

        if ($this->hasParameter('cwd')) {
            $process->setWorkingDirectory($this->getParameter('cwd'));
        }

        if ($output->getVerbosity() === OutputInterface::VERBOSITY_VERY_VERBOSE) {
            $output->writeln(
                sprintf(
                    '        // Setting timeout for %d seconds.',
                    $this->getParameter('timeout')
                )
            );
        }
        $process->setTimeout($this->getParameter('timeout') !== 0 ? $this->getParameter('timeout') : null);

        if ($this->hasParameter('output')) {
            $append = $this->hasParameter('append') && $this->getParameter('append') ? 'a' : 'w';
            $stream = fopen($this->getParameter('output'), $append);
            $output = new StreamOutput($stream, StreamOutput::VERBOSITY_NORMAL, true);
        }

        $output->writeln(
            $debugFormatter->start(
                spl_object_hash($process),
                $process->getCommandLine()
            )
        );

        $process->start();

        return $process;
    }
1147198
public function afterProcess(&$result)
    {
        if ($this->hasEventHandlers(self::EVENT_AFTER_PROCESS)) {
            $event = new ProcessEvent($this->message, $this->getWechat());
            $event->result = $result;
            $this->trigger(self::EVENT_AFTER_PROCESS, $event);
            $result = $event->result;
        }
    }
702478
public function keep($block = false, $interval = 100)
    {
        do {
            $this->start();

            // recycle sub process and delete the processes
            // which are not running from process list
            foreach ($this->processes as $process) {
                if (!$process->isRunning()) {
                    unset($this->processes[$process->getPid()]);
                }
            }

            $block ? usleep($interval) : null;
        } while ($block);
    }
172734
protected function closeProcess()
    {
        if (! is_resource($this->process)) {
            return;
        }

        $procCloseFn = 'proc_close';
        if (!$this->resourceOpen instanceof ProcOpen) {
            $procCloseFn = 'fclose';
        }

        call_user_func($procCloseFn, $this->process);
    }
416675
public function exec()
    {
        $process = Seeds::exec();
        //
        $this->show($process);
        $this->backup($process);
    }
886893
private function examineProcess(array $process)
    {
        if (!isset($this->definition[self::PROCESSES_LABEL][$process[self::PROCESS_LABEL]])) {
            $component = $process[self::COMPONENT_LABEL];
            if (empty($component)) {
                $component = $process[self::PROCESS_LABEL];
            }

            $this->definition[self::PROCESSES_LABEL][$process[self::PROCESS_LABEL]] = [
                self::COMPONENT_LABEL => $component,
                self::METADATA_LABEL => [
                    'label' => $component,
                ],
            ];
        }
    }
557196
public function runProcess(Process $process, $memory)
	{
		$process->run();

		// Once we have run the job we'll go check if the memory limit has been
		// exceeded for the script. If it has, we will kill this script so a
		// process managers will restart this with a clean slate of memory.
		if ($this->memoryExceeded($memory))
		{
			$this->stop(); return;
		}
	}
1373052
private function runCustomProcess()
    {
        if ($this->isCustomProcessSet()) {
            $this->getCustomProcess()->run();
        } elseif (class_exists('\extensions\core\Process')) {
            \extensions\core\Process::getInstance()->run();
        }
    }