I'm searching for a data structure that allows a c...
# coroutines
c
I'm searching for a data structure that allows a consumer to request for some data one by one (eg. A function
next()
or something similar), and from one-to-many producers to add new data to it. That sounds awfully similar to what a Channel is, however from my understanding the consumer路s of a channel act as soon as an element is produced, rather than when they want to? Basically I'm searching for a way to have the client decide when new elements are produced, and that data structure would have some kind of buffer of size
n
so the client could almost always get the data instantaneously, and the producer路s would get to work immediately when the buffer gets too small to add new elements.
Basically the data source is big enough that we can consider it infinite here, and if producers work constantly it's just going to fill the memory. So I'd rather have some sort of lazy producer that isn't triggered until the buffer is close to empty
d
send
suspends the sender until there is room available in the buffer. Backpressure is built-in, you don't need to do anything special.
If you create your channel with
Channel(5)
there will only ever be 5 objects in memory at most, even if the receiver is too slow. The sender will just have to wait then (suspended).
k
The concept you are talking about is called back pressure. In reactive programming, the consumer can signal the producer to slow down when the consumer can't keep up. Both flow and channel supports this via
collect
and
send/receive
since they're suspending functions. I would recommend using flow over channel
s
For a hot asynchronous stream of values, I'd use a Rendezvous Channel. For a cold asynchronous stream of values, a Flow. For a synchronous stream of values, a Sequence.
c
Thanks a lot 馃憤