/** * Run some code in a thread. * * @param callable $func The function to execute * @param mixed $args The arguments to pass to the function * * @return int The pid of the thread created to execute this code */ public function call(callable $func, ...$args) : int { $pid = pcntl_fork(); if ($pid == -1) { throw new Exception("Failed to fork"); } # If the child process was started then return its pid if ($pid) { return $pid; } # If this is the child process, then run the requested function try { $func(...$args); } catch (\Throwable $e) { $this->memory->addException($e); exit(1); } # Then we must exit or else we will end up the child process running the parent processes code die; }