Icicle\Concurrent\Worker\DefaultPool::shutdown PHP Method

shutdown() public method

Shuts down the pool and all workers in it.
public shutdown ( ) : Generator
return Generator
    public function shutdown() : \Generator
    {
        if (!$this->isRunning()) {
            throw new StatusError('The pool is not running.');
        }
        $this->running = false;
        $shutdowns = [];
        foreach ($this->workers as $worker) {
            if ($worker->isRunning()) {
                $shutdowns[] = new Coroutine($worker->shutdown());
            }
        }
        return (yield Awaitable\reduce($shutdowns, function ($carry, $value) {
            return $carry ?: $value;
        }, 0));
    }

Usage Example

示例#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();