A worker pool is a collection of worker threads that can perform multiple tasks simultaneously. The load on each worker is balanced such that tasks are completed as soon as possible and workers are used efficiently.
Inheritance: implements Icicle\Concurrent\Worker\PoolInterface
Exemplo n.º 1
0
 /**
  * {@inheritdoc}
  */
 public function chgrp(string $path, int $gid) : \Generator
 {
     try {
         return yield from $this->pool->enqueue(new Internal\FileTask('chgrp', [$path, $gid]));
     } catch (TaskException $exception) {
         throw new FileException('Creating the directory failed.', $exception);
     }
 }
Exemplo n.º 2
0
#!/usr/bin/env php
<?php
require dirname(__DIR__).'/vendor/autoload.php';

use Icicle\Concurrent\Worker\Pool;
use Icicle\Coroutine\Coroutine;
use Icicle\Examples\Concurrent\BlockingTask;
use Icicle\Loop;
use Icicle\Promise;

$generator = function () {
    $pool = new Pool();
    $pool->start();

    $results = (yield Promise\all([
        'google.com' => new Coroutine($pool->enqueue(new BlockingTask('file_get_contents', 'https://google.com'))),
        'icicle.io'  => new Coroutine($pool->enqueue(new BlockingTask('file_get_contents', 'https://icicle.io'))),
    ]));

    foreach ($results as $source => $result) {
        printf("Read from %s: %d bytes\n", $source, strlen($result));
    }

    yield $pool->shutdown();
};

$coroutine = new Coroutine($generator());
$coroutine->done();

Loop\periodic(0.1, function () {
    printf(".\n");