1
0
mirror of https://github.com/danog/parallel.git synced 2024-11-27 12:54:55 +01:00
parallel/examples/worker-pool.php

35 lines
878 B
PHP
Raw Normal View History

#!/usr/bin/env php
<?php
require dirname(__DIR__).'/vendor/autoload.php';
2015-09-28 05:46:57 +02:00
use Icicle\Concurrent\Worker\Pool;
use Icicle\Coroutine\Coroutine;
2015-09-28 05:46:57 +02:00
use Icicle\Examples\Concurrent\BlockingTask;
use Icicle\Loop;
use Icicle\Promise;
$generator = function () {
2015-09-28 05:46:57 +02:00
$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'))),
]));
2015-09-28 05:46:57 +02:00
foreach ($results as $source => $result) {
printf("Read from %s: %d bytes\n", $source, strlen($result));
}
yield $pool->shutdown();
};
$coroutine = new Coroutine($generator());
$coroutine->done();
2015-09-28 05:46:57 +02:00
Loop\periodic(0.1, function () {
printf(".\n");
})->unreference();
Loop\run();