1
0
mirror of https://github.com/danog/parallel.git synced 2024-12-11 16:49:51 +01:00
parallel/example/worker-pool.php
2016-08-26 09:53:18 -05:00

47 lines
1.3 KiB
PHP
Executable File

#!/usr/bin/env php
<?php
require dirname(__DIR__).'/vendor/autoload.php';
use Amp\Parallel\Worker\DefaultPool;
use Amp\Coroutine;
use Amp\Parallel\Example\BlockingTask;
Amp\execute(function() {
$timer = Amp\repeat(100, function () {
printf(".\n");
});
Amp\unreference($timer);
$pool = new DefaultPool();
$pool->start();
$coroutines = [];
$coroutines[] = function () use ($pool) {
$url = 'https://google.com';
$result = yield $pool->enqueue(new BlockingTask('file_get_contents', $url));
printf("Read from %s: %d bytes\n", $url, strlen($result));
};
$coroutines[] = function () use ($pool) {
$url = 'http://amphp.org';
$result = yield $pool->enqueue(new BlockingTask('file_get_contents', $url));
printf("Read from %s: %d bytes\n", $url, strlen($result));
};
$coroutines[] = function () use ($pool) {
$url = 'https://github.com';
$result = yield $pool->enqueue(new BlockingTask('file_get_contents', $url));
printf("Read from %s: %d bytes\n", $url, strlen($result));
};
$coroutines = array_map(function (callable $coroutine): Coroutine {
return new Coroutine($coroutine());
}, $coroutines);
yield Amp\all($coroutines);
return yield $pool->shutdown();
});