1
0
mirror of https://github.com/danog/parallel.git synced 2024-11-27 04:44:56 +01:00
parallel/examples/worker-pool.php
2015-09-27 22:46:57 -05:00

35 lines
878 B
PHP
Executable File

#!/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");
})->unreference();
Loop\run();