/
worker-pool.php
executable file
·44 lines (34 loc) · 1.27 KB
/
worker-pool.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/env php
<?php
require dirname(__DIR__).'/vendor/autoload.php';
use Icicle\Awaitable;
use Icicle\Concurrent\Worker\DefaultPool;
use Icicle\Coroutine;
use Icicle\Examples\Concurrent\BlockingTask;
use Icicle\Loop;
Coroutine\create(function() {
$pool = new DefaultPool();
$pool->start();
$coroutines = [];
$coroutines[] = Coroutine\create(function () use ($pool) {
$url = 'https://google.com';
$result = yield from $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 from $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://github.com';
$result = yield from $pool->enqueue(new BlockingTask('file_get_contents', $url));
printf("Read from %s: %d bytes\n", $url, strlen($result));
});
yield Awaitable\all($coroutines);
return yield from $pool->shutdown();
})->done();
Loop\periodic(0.1, function () {
printf(".\n");
})->unreference();
Loop\run();