Am I correct in that the only difference between `...
# coroutines
d
Am I correct in that the only difference between
flow { }
and
channelFlow { }
is that the latter is "threadsafe", meaning you can switch dispatcher inside the suspending block? Or is there something else?
🚫 2
o
back pressure also differs, since there is a channel between the sender/emitter and consumer
d
Okay, what does that mean exactly? I understand the concept of backpressure, but how do the two differ in this regard?
a
It means that send returns when the value handoff is complete, contrasted with emit that returns after the downstream collector is done processing it
âž• 5
d
I see. So it would depend on what kind of channel you have "in between", right? If it's a zero-capacity one you effectively have the same as with
emit
, otherwise you can buffer N elements before you need to wait for the collector.
o
yes, and by default it buffers, but you can use
buffer
to change that
âž• 2
d
Thanks to both of you, that clears it up!
z
If it’s a zero-capacity one you effectively have the same as with
emit
Not quite –
offer
still returns as soon as the handoff is complete, which, depending on which dispatcher the downstream is running on, may be before the downstream actually finishes processing that item.
d
So say the downstream is doing some long-running operation after receiving each value. With
emit
upstream would wait for that operation, with channels it would compute the next element, which would then have to wait for downstream (for the next handoff). Right?
z
exactly
âž• 1
d
Thank you
z
but relying on those semantics can make for brittle code – e.g. nothing’s stopping a downstream consumer from putting a channel in the mix. if you actually need synchronization, and not just backpressure, better to synchronize explicitly
d
Yes, obviously. I was just trying to understand the difference here