Scope::cancel

(PHP 8.6+, True Async 1.0)

public function cancel(?AsyncCancellation $cancellationError = null): void

Bricht alle Koroutinen ab, die zum angegebenen Scope gehoeren. Jede aktive Koroutine erhaelt eine CancelledException. Wenn $cancellationError angegeben wird, wird diese als Abbruchgrund verwendet.

Parameter

cancellationError — eine benutzerdefinierte Abbruch-Exception. Wenn null, wird die Standard-CancelledException verwendet.

Rueckgabewert

Es wird kein Wert zurueckgegeben.

Beispiele

Beispiel #1 Einfacher Abbruch

<?php

use Async\Scope;

$scope = new Scope();

$scope->spawn(function() {
    try {
        \Async\delay(60_000); // Lange Operation
    } catch (\Async\CancelledException $e) {
        echo "Koroutine abgebrochen\n";
    }
});

// Alle Koroutinen abbrechen
$scope->cancel();

Beispiel #2 Abbruch mit benutzerdefiniertem Fehler

<?php

use Async\Scope;
use Async\AsyncCancellation;

$scope = new Scope();

$scope->spawn(function() {
    try {
        \Async\delay(60_000);
    } catch (\Async\CancelledException $e) {
        echo "Grund: " . $e->getMessage() . "\n";
    }
});

$error = new AsyncCancellation("Timeout ueberschritten");
$scope->cancel($error);

Siehe auch