AsyncStream: how to batch-read all buffered elements

is there a way to read all buffered elements from an AsyncStream before suspending? i’m dealing with an API that emits large “bursts” of updates in between long periods of inactivity, and reading the elements one-by-one means i am launching a lot of individual tasks that would be more efficiently-run as a batch.

We don't currently have any API for this, but designing one is something I'm highly interested in. I'm not yet at a point where I'm ready to pitch a proposal, but I'd be interested in hearing about your use-case so I can make sure I consider it.

1 Like

it’s kind of a strange use case, as i am reading messages delivered over a TCP connection, where one frame might only contain part of one message, or contain many messages concatenated with each other. my channel handler is parsing and yield-ing each message one at a time, and it does this synchronously (since yield(_:) is blocks efficiently), so, in a single-threaded context, by the time an await-er is ready to consume the messages, there will often be more than one available.

since i control the channel handler, i will probably end up just rewriting it to yield arrays of messages instead of individual messages. but it would be a bigger problem if i was using a library that provides messages one-by-one.

3 Likes
Terms of Service

Privacy Policy

Cookie Policy