A worker pool is a collection of worker threads that can perform multiple tasks simultaneously. The load on each worker is balanced such that tasks are completed as soon as possible and workers are used efficiently.
Inheritance: implements Pool
示例#1
0
    {
        $this->callable = $callable;
        $this->args = $args;
    }
    public function run(Environment $environment)
    {
        ($this->callable)(...$this->args);
    }
}
function wait()
{
    $sleep = rand(1, 200) / 100;
    echo "Sleep {$sleep} seconds\n";
    sleep($sleep);
    echo "Awake\n";
    return true;
}
Coroutine\create(function () {
    $pool = new DefaultPool();
    $pool->start();
    $coroutines = [];
    for ($i = 0; $i < 50; $i++) {
        $coroutines[] = Coroutine\create(function () use($pool) {
            $result = (yield from $pool->enqueue(new CallableTask('wait')));
            return $result;
        });
    }
    (yield Awaitable\all($coroutines));
    return yield from $pool->shutdown();
})->done();
Loop\run();
示例#2
0
#!/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 () {