public function getMessages(): Generator
    {
        if (!$this->pid) {
            throw new BadMethodCallException('getMessages can only be called from the parent of a forked process');
        }

        // Loop through the messages and yield each item in the collection
        foreach ($this->messages as $message) {
            yield $message;
        }
    }