structured concurrency
concurrentに走るTaskが構造化されているからstructured concurrencyと呼ぶらしい.
import Foundation import _Concurrency struct Photo {} func downloadPhoto(named: String) async -> Photo { await Task.sleep(2 * 1_000_000_000) return Photo() } Task { // unstructured concurrency let handle = // structured concurrency Task { // parent task await withTaskGroup(of: Photo.self) { taskGroup in let photoNames = ["a", "b"] for name in photoNames { taskGroup.addTask { // child task return await downloadPhoto(named: name) } } } } // structured concurrency _ = await handle.result // unstructured concurrency }
昨日書いた邪悪な例はこれで善良にかけそうな気がしたけどいま揚げ物していて焦げそうなので明日考える.