Coroutine::isCompleted
(PHP 8.6+, True Async 1.0)
public Coroutine::isCompleted(): bool
Verifica se la coroutine ha terminato l’esecuzione. Una coroutine è considerata completata al termine con successo, al termine con un errore o all’annullamento.
Valore di ritorno
bool – true se la coroutine ha terminato l’esecuzione.
Esempi
Esempio #1 Verifica del completamento
<?php
use function Async\spawn;
use function Async\await;
$coroutine = spawn(function() {
return "test";
});
var_dump($coroutine->isCompleted()); // bool(false)
await($coroutine);
var_dump($coroutine->isCompleted()); // bool(true)
Esempio #2 Controllo di prontezza non bloccante
<?php
use function Async\spawn;
use function Async\suspend;
$tasks = [
spawn(fn() => file_get_contents('https://api1.example.com')),
spawn(fn() => file_get_contents('https://api2.example.com')),
];
// Attendi finché tutti sono completati
while (true) {
$allDone = true;
foreach ($tasks as $task) {
if (!$task->isCompleted()) {
$allDone = false;
break;
}
}
if ($allDone) break;
suspend();
}
Vedi anche
- Coroutine::getResult – Ottieni il risultato
- Coroutine::getException – Ottieni l’eccezione
- Coroutine::isCancelled – Verifica l’annullamento