/** * {@inheritdoc} */ public function receive() : \Generator { if (null === $this->channel) { throw new StatusError('The process has not been started.'); } $data = (yield from $this->channel->receive()); if ($data instanceof ExitStatus) { $data = $data->getResult(); throw new SynchronizationError(sprintf('Thread unexpectedly exited with result of type: %s', is_object($data) ? get_class($data) : gettype($data))); } return $data; }
/** * @coroutine * * @return \Generator */ public function run() : \Generator { $task = (yield from $this->channel->receive()); while ($task instanceof Task) { $this->idle = false; try { $result = (yield $task->run($this->environment)); } catch (\Throwable $exception) { $result = new TaskFailure($exception); } yield from $this->channel->send($result); $this->idle = true; $task = (yield from $this->channel->receive()); } return $task; }
/** * {@inheritdoc} */ public function join() { if (null === $this->channel) { throw new StatusError('The process has not been started.'); } $response = (yield $this->channel->receive()); yield $this->process->join(); if (!$response instanceof ExitStatusInterface) { throw new SynchronizationError('Did not receive an exit status from thread.'); } yield $response->getResult(); }