Esempio n. 1
0
 /**
  * {@inheritdoc}
  */
 public function send($data) : \Generator
 {
     if (null === $this->channel) {
         throw new StatusError('The process has not been started.');
     }
     if ($data instanceof ExitStatus) {
         throw new InvalidArgumentError('Cannot send exit status objects.');
     }
     return yield from $this->channel->send($data);
 }
Esempio n. 2
0
 /**
  * @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;
 }
Esempio n. 3
0
    /**
     * {@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();
    }
Esempio n. 4
0
    exit(1);
}

require $autoloadPath;

use Icicle\Concurrent\Sync\Channel;
use Icicle\Concurrent\Sync\Internal\ExitFailure;
use Icicle\Concurrent\Sync\Internal\ExitSuccess;
use Icicle\Concurrent\Worker\Environment;
use Icicle\Concurrent\Worker\Internal\TaskRunner;
use Icicle\Coroutine;
use Icicle\Loop;
use Icicle\Stream;

Coroutine\create(function () {
    $channel = new Channel(Stream\stdin(), Stream\stdout());
    $environment = new Environment();

    $runner = new TaskRunner($channel, $environment);

    try {
        $result = new ExitSuccess(yield $runner->run());
    } catch (Exception $exception) {
        $result = new ExitFailure($exception);
    }

    yield $channel->send($result);
})->done();

Loop\run();