<https://kotlinlang.org/docs/flow.html#buffering> ...
# coroutines
k
https://kotlinlang.org/docs/flow.html#buffering – why does it take 1000ms to complete the buffered flow and not ~600ms? I thought `.buffer(
and/or
.flowOn`would result in parallelism so in my mind it'd take 300ms for
simple
to complete and then again 300 ms to process the last element of
simple
=> ~600ms to complete everything.
e
There are 3 elements. Each takes 300 ms to process. So 3 * 300 ms = 900 ms just for processing. Add to that the fact, that the first element gets emitted only after 100 ms pass. You get 1000 ms
j
Since
collect
on the consumer side contains a
delay(300)
, and 3 elements are collected, it needs at least 900ms to complete. If you add the 100ms that was needed by the source flow to emit the first element and unlock the first collection iteration, you have 1000ms
Close enough 😄
e
buffer
is not processing elements in parallel. It makes emitter before
buffer
and collector after
buffer
to run in parallel to each other. (this might be the source of the original confusion)
k
Yeah, sounds like it. Right now I try to migrate some of my Reactor code to coroutines … how would you write a flow runs database calls in parallel on the flows items? In reactor this would be
.flatMap
on another
Mono
.