コード例 #1
0
 /**
  * @param \Icicle\Concurrent\Worker\Pool|null $pool
  */
 public function __construct(Pool $pool = null)
 {
     $this->pool = $pool ?: Worker\pool();
     if (!$this->pool->isRunning()) {
         $this->pool->start();
     }
 }
コード例 #2
0
ファイル: worker-pool.php プロジェクト: Nik-ADA/concurrent
#!/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");