ThreadPool::getPendingCount()
(PHP 8.6+, True Async 1.0)
php
public ThreadPool::getPendingCount(): intRestituisce il numero di task che sono stati inviati ma non ancora prelevati da un thread worker. Questo contatore è supportato da una variabile atomica ed è accurato in qualsiasi momento, anche mentre i worker sono in esecuzione in parallelo.
Valore restituito
int — numero di task attualmente in attesa nella coda.
Esempi
Esempio #1 Osservare lo svuotamento della coda
php
<?php
use Async\ThreadPool;
use function Async\spawn;
use function Async\await;
use function Async\delay;
spawn(function() {
$pool = new ThreadPool(workers: 2);
$futures = [];
for ($i = 0; $i < 6; $i++) {
$futures[] = $pool->submit(function() {
$t = microtime(true);
while (microtime(true) - $t < 0.1) {}
return 'done';
});
}
delay(10); // lasciare tempo ai worker di avviarsi
echo "in attesa: ", $pool->getPendingCount(), "\n"; // in attesa: 4
foreach ($futures as $f) {
await($f);
}
echo "in attesa: ", $pool->getPendingCount(), "\n"; // in attesa: 0
$pool->close();
});Vedere anche
- ThreadPool::getRunningCount() — task attualmente in esecuzione
- ThreadPool::getCompletedCount() — task totali completati
- ThreadPool::getWorkerCount() — numero di worker
- Async\ThreadPool — panoramica del componente