TaskGroup::cancel
(PHP 8.6+, True Async 1.0)
public TaskGroup::cancel(?Async\AsyncCancellation $cancellation = null): void
Cancels all running coroutines and queued tasks.
Implicitly calls seal(). Queued tasks are never started.
Coroutines receive an AsyncCancellation and terminate.
Cancellation happens asynchronously — use awaitCompletion() to guarantee completion.
Parameters
- cancellation
- The exception serving as the cancellation reason. If
null, a standardAsyncCancellationwith the message “TaskGroup cancelled” is used.
Examples
Example #1 Cancellation with waiting for completion
<?php
use Async\TaskGroup;
spawn(function() {
$group = new TaskGroup();
$group->spawn(function() {
Async\delay(10000);
return "long task";
});
$group->cancel();
$group->awaitCompletion();
echo "all tasks cancelled\n";
});
Example #2 Cancellation with a reason
<?php
use Async\TaskGroup;
spawn(function() {
$group = new TaskGroup();
$group->spawn(fn() => Async\delay(10000));
$group->cancel(new \Async\AsyncCancellation("Timeout exceeded"));
$group->awaitCompletion();
});
See Also
- TaskGroup::seal — Seal without cancellation
- TaskGroup::awaitCompletion — Wait for completion
- TaskGroup::dispose — Dispose of the group scope