Hello developers, I already see parallel code using constant number of parallel like this from official site.
async let firstPhoto = downloadPhoto(named: photoNames[0])
async let secondPhoto = downloadPhoto(named: photoNames[1])
async let thirdPhoto = downloadPhoto(named: photoNames[2])
let photos = await [firstPhoto, secondPhoto, thirdPhoto]
show(photos)
But I want to use this to variable size of works.
for photoName in photoNames {
async let ... = downloadPhoto(named: photoName)
}
let photos = await ...
show(photos)
Does swift 5.5 support this case???
By any chance, do I use TaskGroup
?
I use TaskGroup already, but I want to know that swift supports it.
Seems you have read the Concurrency part of the Swift book, but it does not tell much about the current restrictions of async let
.
Does swift 5.5 support this case???
If you mean using async let
inside a code block of looping statement such as for-in, the answer is no.
In the accepted proposal SE-0317 async let bindings, you can find this description:
Specifically,
async let
declarations are not able to express dynamic numbers of tasks executing in parallel, like this group showcases:func toyParallelMap<A, B>(_ items: [A], f: (A) async -> B) async -> [B] { return await withTaskGroup(of: (Int, B).self) { group in var bs = [B?](repeating: nil, count: items.count) // spawn off processing all `f` mapping functions in parallel // in reality, one might want to limit the "width" of these for i in items.indices { group.async { (i, await f(items[i])) } } // collect all results for await (i, mapped) in group { bs[i] = mapped } return bs.map { $0! } } }
In the above
toyParallelMap
the number of child-tasks is dynamic because it depends on the count of elements in theitems
array at runtime. Such patterns are not possible to express usingasync let
because we'd have to know how manyasync let
declarations to create at compile time.
I use TaskGroup already, but I want to know that swift supports it.
Seems you need to go on with TaskGroup
in cases such as described in your opening post.