TaskGroup::seal

(PHP 8.6+, True Async 1.0)

public TaskGroup::seal(): void

密封组。之后使用 spawn()spawnWithKey() 将抛出异常。 已运行的协程和排队的任务继续执行。

重复调用不会有任何效果。

示例

示例 #1 基本用法

<?php

use Async\TaskGroup;

spawn(function() {
    $group = new TaskGroup();

    $group->spawn(fn() => "task");
    $group->seal();

    try {
        $group->spawn(fn() => "another task");
    } catch (\Async\AsyncException $e) {
        echo $e->getMessage() . "\n";
        // "Cannot spawn tasks on a sealed TaskGroup"
    }
});

参见