...point of fact, I am aware that multi-threaded asyncio doesn't happen out of the box in python and that it takes work to marshall a call into an executor. My point is that the moment of await is the synchronization point between the two threads.
Post
Replies
Boosts
Views
Activity
I stand corrected. TaskGroup seems like the way to go. But there's definitely an opportunity to build an api that removes some boilerplate (a la bluebirdjs.com):
func<T> all(tasks: WhateverTaskType[]) async throws -> T[] {
var arr: Array<T> = []
try await withThrowingTaskGroup(of: T.self) { group in
for task in tasks {
group.async(operation: task)
}
for try await val in group {
arr.append(val)
}
}
return arr
}
The next documentation seems to suggest that it only awaits one of the child tasks, not all of them in parallel. I guess an empty for loop iterating over any AsyncSequence, which uses the next() method, achieves the same thing with a little more boilerplate. After the for loop completes, all the tasks are guaranteed to have completed. In many cases, that's probably enough.