ComponentsThreadPool::getWorkerCount()

ThreadPool::getWorkerCount()

(PHP 8.6+, True Async 1.0)

php
public ThreadPool::getWorkerCount(): int

Returns the number of worker threads in the pool. This value is fixed at construction time and does not change during the pool's lifetime. It equals the $workers argument passed to new ThreadPool().

Return Value

int — number of worker threads (as set in the constructor).

Examples

Example #1 Confirming worker count

php
<?php

use Async\ThreadPool;
use function Async\spawn;

spawn(function() {
    $pool = new ThreadPool(workers: 4);

    echo $pool->getWorkerCount(), "\n"; // 4

    $pool->close();
});

Example #2 Sizing the pool to available CPU cores

php
<?php

use Async\ThreadPool;
use function Async\spawn;
use function Async\await;

spawn(function() {
    $cores = (int) shell_exec('nproc') ?: 4;
    $pool  = new ThreadPool(workers: $cores);

    echo "Pool created with ", $pool->getWorkerCount(), " workers\n";

    $futures = [];
    for ($i = 0; $i < $cores * 2; $i++) {
        $futures[] = $pool->submit(fn() => 'done');
    }
    foreach ($futures as $f) {
        await($f);
    }

    $pool->close();
});

See Also