Concurrency Coordination Reports
Async Sequence Coordination Report
Record items drained from an async sequence and report the consumption state.
Async Sequence Coordination Report
async_sequence_coordination_report.swift
func drain(_ n: Int) async -> (Int, Int) {
let stream = AsyncStream<Int> { continuation in
for i in 0..<n {
continuation.yield(i + 1)
}
continuation.finish()
}
var drained = 0
var total = 0
for await value in stream {
drained += 1
total += value
}
return (drained, total)
}
let items =
let result = await drain(items)
let drained = result.0
let total = result.1
var status = "draining"
if drained == 0 {
status = "empty"
}
if drained >= 4 {
status = "full"
}
let line = "items=\(items) drained=\(drained) total=\(total) \(status)"
print(line)
func drain(_ n: Int) async -> (Int, Int) {
let stream = AsyncStream<Int> { continuation in
for i in 0..<n {
continuation.yield(i + 1)
}
continuation.finish()
}
var drained = 0
var total = 0
for await value in stream {
drained += 1
total += value
}
return (drained, total)
}
let items =
let result = await drain(items)
let drained = result.0
let total = result.1
var status = "draining"
if drained == 0 {
status = "empty"
}
if drained >= 4 {
status = "full"
}
let line = "items=\(items) drained=\(drained) total=\(total) \(status)"
print(line)
func drain(_ n: Int) async -> (Int, Int) {
let stream = AsyncStream<Int> { continuation in
for i in 0..<n {
continuation.yield(i + 1)
}
continuation.finish()
}
var drained = 0
var total = 0
for await value in stream {
drained += 1
total += value
}
return (drained, total)
}
let items =
let result = await drain(items)
let drained = result.0
let total = result.1
var status = "draining"
if drained == 0 {
status = "empty"
}
if drained >= 4 {
status = "full"
}
let line = "items=\(items) drained=\(drained) total=\(total) \(status)"
print(line)
async sequence drain
A coordination report can record how an async sequence was consumed. An `AsyncStream` yields a fixed run of values and then finishes, and a single `for await` loop drains it to completion, counting each value as it arrives. The whole drain is awaited inside one async function, so the counts are settled before they are reported. With no items the stream is empty, with a short run it is still draining, and with a full run every value is consumed. The status line summarizes the mode while the total confirms the drained values.