Coroutine::isCancelled

(PHP 8.6+, True Async 1.0)

public Coroutine::isCancelled(): bool

Prueft, ob die Coroutine abgebrochen und abgeschlossen wurde. Gibt nur dann true zurueck, wenn der Abbruch vollstaendig abgeschlossen ist.

Befindet sich die Coroutine innerhalb von protect(), gibt isCancelled() false zurueck, bis der geschuetzte Abschnitt abgeschlossen ist, auch wenn cancel() bereits aufgerufen wurde. Um eine Abbruchanforderung zu pruefen, verwenden Sie isCancellationRequested().

Rueckgabewert

booltrue, wenn die Coroutine abgebrochen und abgeschlossen wurde.

Beispiele

Beispiel #1 Einfacher Abbruch

<?php

use function Async\spawn;
use function Async\suspend;

$coroutine = spawn(function() {
    Async\delay(10000);
});

suspend();

$coroutine->cancel();

suspend(); // Abbruch abschliessen lassen

var_dump($coroutine->isCancelled()); // bool(true)
var_dump($coroutine->isCompleted()); // bool(true)

Beispiel #2 Aufgeschobener Abbruch mit protect()

<?php

use function Async\spawn;
use function Async\suspend;
use function Async\protect;

$coroutine = spawn(function() {
    protect(function() {
        // Kritischer Abschnitt -- Abbruch wird aufgeschoben
        Async\delay(100);
    });
});

suspend();

$coroutine->cancel();

// Abbruch angefordert, aber noch nicht abgeschlossen
var_dump($coroutine->isCancellationRequested()); // bool(true)
var_dump($coroutine->isCancelled());             // bool(false)

suspend(); // protect() abschliessen lassen

var_dump($coroutine->isCancelled());             // bool(true)

Siehe auch