Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TaskGroup `async` vs. `spawn`

Does anyone know, in the new Swift async/await stuff, is there any difference between TaskGroup async and spawn, or are they pure synonyms? (If they are synonyms, I rather like spawn better. async looks like we're opening an async block, and that's not what we're doing at all.)

https://developer.apple.com/documentation/swift/taskgroup/3814850-async

https://developer.apple.com/documentation/swift/taskgroup/3814884-spawn

like image 839
matt Avatar asked Jun 15 '21 04:06

matt


People also ask

When should I not use a task group?

All child tasks added to a task group start running automatically and concurrently. We cannot control when a child task finishes its execution. Therefore, we should not use a task group if we want the child tasks to finish in a specific order. A task group only returns when all of its child tasks finish their execution.

What is a task group in Swift?

Apple introduced task groups in Swift 5.5 as one of the essential parts in the Swift concurrency framework. As the name implies, a task group is a collection of child tasks that run concurrently, and it only returns when all of its child tasks finish executing.

How do I create a task group from a task?

A group that contains dynamically created child tasks. To create a task group, call the withTaskGroup (of:returning:body:) method. Don’t use a task group from outside the task where you created it.

What is a task group in Salesforce?

As the name implies, a task group is a collection of child tasks that run concurrently, and it only returns when all of its child tasks finish executing. In this article, I would like to show you how to create a task group, add child tasks to a task group, and gather results from all the child tasks.


1 Answers

SE-0304, tells us that spawn was renamed async as part of the second review:

  • TaskGroup.spawn and TaskGroup.spawnUnlessCancelled have been renamed to TaskGroup.async and TaskGroup.asyncUnlessCancelled which are to be their final names. This aligns the naming with the renamed async let as the word signifying creation of a child task.

The portion in italics has subsequently been removed and the third review has renamed it again:

  • renamed TaskGroup.async and TaskGroup.asyncUnlessCancelled to TaskGroup.addTask and TaskGroup.addTaskUnlessCancelled. The fundamental behavior here is that we're adding a task to the group. add by itself does not suffice, because we aren't adding a value (accessible via next()), we are adding a task whose value will be accessible via next(). It also parallels the use of Task { ... } to create top-level tasks.
like image 133
Rob Avatar answered Oct 17 '22 17:10

Rob