Hi! I'm coming from Spring Webflux/Reactor and now...
# coroutines
k
Hi! I'm coming from Spring Webflux/Reactor and now I try to orchestrate a lot of asynchronous I/O with Flows. Currently it seems that my flows behave rather sequential. Could someone point out what the proper way to do something like
Flux.just(…).flatMap { someAsnyStuff(it) }.subscribe()
would be with a Flow? It seems that
myFlow.flatMapMerge { someSuspendingStuff(it) }.collect()
does not result in the same concurrency/throughput 😕
j
I don't think your suggested code compiles, unless you have suspending functions returning flows, which is a bit weird. But anyway, whether these calls will run in parallel or not depends on the dispatcher used in the coroutine scope where you collect this flow, and/or the dispatcher used in
someSuspendingStuff
if it switches context
k
@Joffrey OK, thanks. Could you modify the exmaple to point out what I would be missing here?
j
You could make single element flows:
myFlow.flatMapMerge { flowOf(someSuspendingStuff(it)) }.collect()
. This would make the collection of each element concurrent. But if you're after parallelism what I'm saying is that it will depend on the coroutine scope in which you're running this, and also depends on what's inside
someSuspendingStuff
.
k
OK, let me dig into that. Thanks. And yeah, the function returns a flow. That's it. Sorry for my bad example