TaskSet::cancel

(PHP 8.6+, True Async 1.0)

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

Cancels all running coroutines and clears the task queue. Implicitly calls seal().

Parameters

cancellation
Cancellation reason. If null, a default AsyncCancellation is created.

Examples

Example #1 Conditional cancellation

<?php

use Async\TaskSet;

spawn(function() {
    $set = new TaskSet();

    $set->spawn(fn() => longRunningTask1());
    $set->spawn(fn() => longRunningTask2());

    // Cancel all tasks
    $set->cancel();

    echo $set->isSealed() ? "sealed\n" : "no\n"; // "sealed"
});

See Also