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

45 lines
1.3 KiB
PHP
Raw Normal View History

#!/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;
use Icicle\Coroutine;
2015-09-28 05:46:57 +02:00
use Icicle\Examples\Concurrent\BlockingTask;
use Icicle\Loop;
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';
2016-01-23 07:00:56 +01:00
$result = yield from $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));
});
$coroutines[] = Coroutine\create(function () use ($pool) {
$url = 'https://icicle.io';
2016-01-23 07:00:56 +01:00
$result = yield from $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-01-15 01:16:07 +01:00
$coroutines[] = Coroutine\create(function () use ($pool) {
$url = 'https://github.com';
2016-01-23 07:00:56 +01:00
$result = yield from $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));
});
2015-09-28 05:46:57 +02:00
2016-01-15 01:16:07 +01:00
yield Awaitable\all($coroutines);
2016-01-23 07:00:56 +01:00
return yield from $pool->shutdown();
})->done();
2015-09-28 05:46:57 +02:00
Loop\periodic(0.1, function () {
printf(".\n");
})->unreference();
Loop\run();