ThreadPool::getWorkerCount()
(PHP 8.6+, True Async 1.0)
php
public ThreadPool::getWorkerCount(): int返回池中工作线程的数量。该值在构造时固定,在池的整个生命周期内不会改变。它等于传递给 new ThreadPool() 的 $workers 参数值。
返回值
int — 工作线程数量(与构造函数中设置的一致)。
示例
示例 #1 确认工作线程数量
php
<?php
use Async\ThreadPool;
use function Async\spawn;
spawn(function() {
$pool = new ThreadPool(workers: 4);
echo $pool->getWorkerCount(), "\n"; // 4
$pool->close();
});示例 #2 根据可用 CPU 核心数调整池大小
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();
});参见
- ThreadPool::getPendingCount() — 在队列中等待的任务
- ThreadPool::getRunningCount() — 当前正在执行的任务
- ThreadPool::getCompletedCount() — 已完成的任务总数
- Async\ThreadPool — 组件概述