Coroutine::cancel

(PHP 8.6+, True Async 1.0)

public Coroutine::cancel(?Async\AsyncCancellation $cancellation = null): void

Bricht die Ausfuehrung der Coroutine ab. Die Coroutine erhaelt eine AsyncCancellation-Ausnahme am naechsten Unterbrechungspunkt (suspend, await, delay usw.).

Der Abbruch funktioniert kooperativ – die Coroutine wird nicht sofort unterbrochen. Befindet sich die Coroutine innerhalb von protect(), wird der Abbruch aufgeschoben, bis der geschuetzte Abschnitt abgeschlossen ist.

Parameter

cancellation
Die Ausnahme, die als Abbruchgrund dient. Bei null wird eine Standard-AsyncCancellation erstellt.

Beispiele

Beispiel #1 Einfacher Abbruch

<?php

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

$coroutine = spawn(function() {
    try {
        Async\delay(10000);
    } catch (\Async\AsyncCancellation $e) {
        echo "Abgebrochen: " . $e->getMessage() . "\n";
    }
});

suspend();

$coroutine->cancel();

await($coroutine);

Beispiel #2 Abbruch mit Grund

<?php

use function Async\spawn;
use function Async\await;

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

$coroutine->cancel(new \Async\AsyncCancellation("Timeout ueberschritten"));

try {
    await($coroutine);
} catch (\Async\AsyncCancellation $e) {
    echo $e->getMessage() . "\n"; // "Timeout ueberschritten"
}

Beispiel #3 Abbruch vor dem Start

<?php

use function Async\spawn;
use function Async\await;

$coroutine = spawn(function() {
    return "sollte nicht abgeschlossen werden";
});

// Abbrechen, bevor der Scheduler die Coroutine startet
$coroutine->cancel();

try {
    await($coroutine);
} catch (\Async\AsyncCancellation $e) {
    echo "Coroutine vor dem Start abgebrochen\n";
}

Siehe auch