2015-08-28 05:51:50 +02:00
|
|
|
#!/usr/bin/env php
|
|
|
|
<?php
|
|
|
|
require dirname(__DIR__).'/vendor/autoload.php';
|
|
|
|
|
2016-08-18 18:04:48 +02:00
|
|
|
use Amp\Coroutine;
|
2017-02-18 18:06:03 +01:00
|
|
|
use Amp\Parallel\{ Example\BlockingTask, Worker\DefaultPool };
|
2017-01-09 18:11:25 +01:00
|
|
|
use AsyncInterop\Loop;
|
2015-08-28 05:51:50 +02:00
|
|
|
|
2016-12-30 02:17:26 +01:00
|
|
|
Loop::execute(Amp\wrap(function() {
|
|
|
|
$timer = Loop::repeat(100, function () {
|
2016-08-18 18:04:48 +02:00
|
|
|
printf(".\n");
|
|
|
|
});
|
2016-12-30 02:17:26 +01:00
|
|
|
Loop::unreference($timer);
|
2016-08-18 18:04:48 +02:00
|
|
|
|
2017-02-18 18:06:03 +01:00
|
|
|
$pool = new DefaultPool;
|
2015-09-28 05:46:57 +02:00
|
|
|
$pool->start();
|
|
|
|
|
2016-01-15 01:16:07 +01:00
|
|
|
$coroutines = [];
|
|
|
|
|
2016-08-18 18:04:48 +02:00
|
|
|
$coroutines[] = function () use ($pool) {
|
2016-01-15 01:16:07 +01:00
|
|
|
$url = 'https://google.com';
|
2016-08-18 18:04:48 +02:00
|
|
|
$result = yield $pool->enqueue(new BlockingTask('file_get_contents', $url));
|
2016-01-15 01:16:07 +01:00
|
|
|
printf("Read from %s: %d bytes\n", $url, strlen($result));
|
2016-08-18 18:04:48 +02:00
|
|
|
};
|
2016-01-15 01:16:07 +01:00
|
|
|
|
2016-08-18 18:04:48 +02:00
|
|
|
$coroutines[] = function () use ($pool) {
|
|
|
|
$url = 'http://amphp.org';
|
|
|
|
$result = yield $pool->enqueue(new BlockingTask('file_get_contents', $url));
|
2016-01-15 01:16:07 +01:00
|
|
|
printf("Read from %s: %d bytes\n", $url, strlen($result));
|
2016-08-18 18:04:48 +02:00
|
|
|
};
|
2015-08-28 05:51:50 +02:00
|
|
|
|
2016-08-18 18:04:48 +02:00
|
|
|
$coroutines[] = function () use ($pool) {
|
2016-01-15 01:16:07 +01:00
|
|
|
$url = 'https://github.com';
|
2016-08-18 18:04:48 +02:00
|
|
|
$result = yield $pool->enqueue(new BlockingTask('file_get_contents', $url));
|
2016-01-15 01:16:07 +01:00
|
|
|
printf("Read from %s: %d bytes\n", $url, strlen($result));
|
2016-08-18 18:04:48 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
$coroutines = array_map(function (callable $coroutine): Coroutine {
|
|
|
|
return new Coroutine($coroutine());
|
|
|
|
}, $coroutines);
|
2016-01-15 01:16:07 +01:00
|
|
|
|
2016-08-18 18:04:48 +02:00
|
|
|
yield Amp\all($coroutines);
|
2015-08-28 05:51:50 +02:00
|
|
|
|
2016-08-18 18:04:48 +02:00
|
|
|
return yield $pool->shutdown();
|
2016-12-30 02:17:26 +01:00
|
|
|
}));
|
2015-09-28 05:46:57 +02:00
|
|
|
|