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

start() public method

When the worker pool starts up, the minimum number of workers will be created. This adds some overhead to starting the pool, but allows for greater performance during runtime.
public start ( )
    public function start()
    {
        if ($this->isRunning()) {
            throw new StatusError('The worker pool has already been started.');
        }
        // Start up the pool with the minimum number of workers.
        $count = $this->minSize;
        while (--$count >= 0) {
            $worker = $this->createWorker();
            $this->idleWorkers->enqueue($worker);
        }
        $this->running = true;
    }

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();