2015-08-28 05:51:50 +02:00
|
|
|
#!/usr/bin/env php
|
|
|
|
<?php
|
|
|
|
require dirname(__DIR__).'/vendor/autoload.php';
|
|
|
|
|
2015-12-05 06:50:32 +01:00
|
|
|
use Icicle\Awaitable;
|
|
|
|
use Icicle\Concurrent\Worker\DefaultPool;
|
2015-12-12 05:44:51 +01:00
|
|
|
use Icicle\Coroutine;
|
2015-09-28 05:46:57 +02:00
|
|
|
use Icicle\Examples\Concurrent\BlockingTask;
|
2015-08-28 05:51:50 +02:00
|
|
|
use Icicle\Loop;
|
|
|
|
|
2015-12-12 05:44:51 +01:00
|
|
|
Coroutine\create(function() {
|
2015-12-05 06:50:32 +01:00
|
|
|
$pool = new DefaultPool();
|
2015-09-28 05:46:57 +02:00
|
|
|
$pool->start();
|
|
|
|
|
2016-01-15 01:16:07 +01:00
|
|
|
$coroutines = [];
|
|
|
|
|
|
|
|
$coroutines[] = Coroutine\create(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[] = Coroutine\create(function () use ($pool) {
|
|
|
|
$url = 'https://icicle.io';
|
|
|
|
$result = (yield $pool->enqueue(new BlockingTask('file_get_contents', $url)));
|
|
|
|
printf("Read from %s: %d bytes\n", $url, strlen($result));
|
2015-12-12 05:44:51 +01:00
|
|
|
});
|
2015-08-28 05:51:50 +02:00
|
|
|
|
2016-01-15 01:16:07 +01:00
|
|
|
$coroutines[] = Coroutine\create(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));
|
2015-12-12 05:44:51 +01:00
|
|
|
});
|
2015-09-28 05:46:57 +02:00
|
|
|
|
2016-01-15 01:16:07 +01:00
|
|
|
yield Awaitable\all($coroutines);
|
|
|
|
|
2015-09-28 05:46:57 +02:00
|
|
|
yield $pool->shutdown();
|
2015-12-12 05:44:51 +01:00
|
|
|
})->done();
|
2015-08-28 05:51:50 +02:00
|
|
|
|
2015-09-28 05:46:57 +02:00
|
|
|
Loop\periodic(0.1, function () {
|
|
|
|
printf(".\n");
|
|
|
|
})->unreference();
|
|
|
|
|
2015-08-28 05:51:50 +02:00
|
|
|
Loop\run();
|