available_parallelism
(PHP 8.6+, True Async 1.0)
Async\available_parallelism() returns the number of CPUs available to the current process.
Description
namespace Async;
function available_parallelism(): intHonours cgroup CPU quotas, sched_setaffinity, and similar constraints. This is the value libuv recommends for thread-pool / worker-pool sizing. Always >= 1.
In a container with cpu.max=2, the function returns 2, not the physical core count of the host. On bare metal — the number of logical cores minus affinity restrictions (if any).
Backend: uv_available_parallelism() with a fallback to uv_cpu_info.
Return value
int — number of CPUs, guaranteed >= 1.
Examples
Example #1 Pool size matching available CPUs
<?php
use Async\ThreadPool;
use function Async\available_parallelism;
// Idiomatic: the autodetect is already built into ThreadPool via workers=0,
// but the explicit call is useful when you scale something else.
$pool = new ThreadPool(workers: available_parallelism());Example #2 HTTP server worker-pool size
<?php
use TrueAsync\HttpServer;
use TrueAsync\HttpServerConfig;
use function Async\available_parallelism;
$server = new HttpServer(
(new HttpServerConfig())
->addListener('0.0.0.0', 8080)
->setWorkers(available_parallelism())
);
$server->start();Example #3 Environment diagnostics
<?php
use function Async\available_parallelism;
echo "Process can use ", available_parallelism(), " CPU(s)\n";
// In Docker with `--cpus=2` → 2
// On a host with 16 cores and no limits → 16
// In a Kubernetes pod with requests/limits cpu=1 → 1Notes
Tip: for
ThreadPoolandHttpServer::setWorkers()you do not have to call this function by hand at all — both components useavailable_parallelism()automatically when the pool size is set to0.
On most IO-bound workloads it makes sense to overcommit by
N + 1orN + 2, because some workers will be blocked on I/O.
See also
- Async\ThreadPool — where the value is used automatically
- Async\cpu_usage() — current process and system load
- Async\loadavg() — average run-queue length