我正在尝试执行一系列网络请求,并希望在新的 Swift 并发系统中限制并行任务的数量。在操作队列中,我们会使用maxConcurrentOperationCount
。在 Combine 中,使用flatMap(maxPublishers:_:)
。在新的 Swift 并发系统中有什么等效的方法?
例如,这可能不是特别相关,但考虑一下:
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()
}
}
这导致所有请求同时运行。 事实上,
URLSession
不遵守httpMaximumConnectionsPerHost
的规定确实很有趣,但这并不是关键问题。我更关心的是如何在一系列并行运行的异步任务中限制并发程度。
os_signpost
调用。 - Robos_signpost
调用来标记每个网络请求的.begin
和.end
)。https://i.stack.imgur.com/xR4Ii.png - RobAsyncChannel
。例如,请参见 https://dev59.com/5cTsa4cB1Zd3GeqPINX7#73072799 或 https://dev59.com/I8Pra4cB1Zd3GeqPt_11#75730483 的示例。 - Rob