I am trying to perform a series of network requests and would like to limit the number of concurrent tasks in the new Swift Concurrency system. With operation queues we would use maxConcurrentOperationCount
. In Combine, flatMap(maxPublishers:_:)
. What is the equivalent in the new Swift Concurrency system?
E.g., it is not terribly relevant, but consider:
func downloadAll() async throws {
try await withThrowingTaskGroup(of: Void.self) { group in
for index in 0..<20 {
group.addTask { try await self.download(index) }
}
try await group.waitForAll()
}
}
That results in all the requests running concurrently:
The fact that URLSession
is not honoring httpMaximumConnectionsPerHost
is interesting, but not the salient issue here. I am looking for, more generally, how to constrain the degree of concurrency in a series of asynchronous tasks running in parallel.