Discussion 解説
A group waits for all of its child tasks to complete or be canceled before it returns. After this function returns, the task group is always empty.
To collect the results of the group’s child tasks, you can use a for
-await
-in
loop:
If you need more control or only a few results, you can call next()
directly:
Task Group Cancellation タスクグループ取り消し
You can cancel a task group and all of its child tasks by calling the cancell
method on the task group, or by canceling the task in which the group is running.
If you call async(priority:
to create a new task in a canceled group, that task is immediately canceled after creation.
あなたがasync(priority:
を呼び出すことで新しいタスクをある取り消されたグループの中に作成するならば、そのタスクは作成の後に直ぐに取り消されます。
Alternatively, you can call async
, which doesn’t create the task if the group has already been canceled Choosing between these two functions lets you control how to react to cancellation within a group: some child tasks need to run regardless of cancellation, but other tasks are better not even being created when you know they can’t produce useful results.
Because the tasks you add to a group with this method are nonthrowing, those tasks can’t respond to cancellation by throwing Cancellation
. The tasks must handle cancellation in some other way, such as returning the work completed so far, returning an empty result, or returning nil
. For tasks that need to handle cancellation by throwing an error, use the with
method instead.
あなたがグループにこのメソッドで加えるタスクはスローしないことから、それらタスクは、Cancellation
をスローすることによって取り消しに応答できません。タスクは取り消しを何らかの他の方法で取り扱わなければなりません、たとえばそれまでに完了された仕事を返す、空の結果を返す、またはnil
を返すなど。エラーをスローすることによって取り消しを取り扱う必要があるタスクのために、with
メソッドを代わりに使ってください。