1
0
mirror of https://github.com/danog/parallel.git synced 2024-12-02 09:37:57 +01:00
parallel/examples/worker-pool.php
2016-08-18 17:36:58 -05:00

47 lines
1.3 KiB
PHP
Executable File

#!/usr/bin/env php
<?php
require dirname(__DIR__).'/vendor/autoload.php';
use Amp\Concurrent\Worker\DefaultPool;
use Amp\Coroutine;
use Amp\Concurrent\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();
});