主要使用pcntl_fork(),改函数执行的时候,会创建一个子进程.
eg:
/**
* Execute user functions in multiThreads mode
*
* @param \Traversable $userFunctions
* @throws \RuntimeException
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
*/
private function multiThreadsExecute($userFunctions)
{
$this->resource->closeConnection(null);
$threadNumber = 0;
foreach ($userFunctions as $userFunction) {
// phpcs:ignore Magento2.Functions.DiscouragedFunction
$pid = pcntl_fork();
if ($pid == -1) {
throw new \RuntimeException('Unable to fork a new process');
} elseif ($pid) {
$this->executeParentProcess($threadNumber);
} else {
$this->startChildProcess($userFunction);
}
}
// phpcs:ignore Magento2.CodeAnalysis.EmptyBlock,Magento2.Functions.DiscouragedFunction
while (pcntl_waitpid(0, $status) != -1) {
//Waiting for the completion of child processes
}
if ($this->failInChildProcess) {
throw new \RuntimeException('Fail in child process');
}
}