altavir
04/26/2019, 11:25 AMFlow
map operation parallelizable?gildor
04/26/2019, 11:38 AMaltavir
04/26/2019, 11:40 AMDeferred
and then await them in next mapfun <T,R> Flow<T>.mapParallel(scope: CoroutineScope, transform: suspend (T)->R) = map{
scope.async { transform(it) }
}.map{
it.await()
}
gildor
04/26/2019, 3:02 PMI will need to create single-element flowsThere are extension functions for functional types (including suspend) which create flow from lambda/function
altavir
04/26/2019, 3:04 PMflatMapMerge
does not provide control over scope and dispatcher, so it could give a work around at best. I think this is rather common task so it should have dedicated solution.elizarov
04/26/2019, 6:10 PM.flowWith(Dispatchers.Default) { // BG!
flatMapMerge {
flowOf(slowOperation(it))
}
}
altavir
04/26/2019, 6:13 PMflowWith
that skips channel generation in case context is the sameasync
, store them and then await
them. In my solution channel plays the role of interediate storage, but I do not think any solution with flowOn
would generate this intermediate state because, channel there is consumed immediately.elizarov
04/26/2019, 6:57 PMsdeleuze
04/28/2019, 9:03 AMmap
and flatMapMerge/Concat
which forces to use artificially a single element Flow
to achieve parrallel execution? Processing each element of a Flow
with slow operations (remote http call for each element) is very common.flatMap
in the doc seems misleading to me, I think most people want parrallel execution when calling suspending function to transform element of a Flow
map
could do parrallel transformation when used with suspending functions?altavir
04/28/2019, 10:57 AMgildor
04/28/2019, 3:19 PMmap
parallel, even do this optional (add concurrency param with default value 1), so it will use fast path by default (current implementation) and optional easy to use concurrencysdeleuze
04/28/2019, 3:25 PMmap
optional parameter, and I think I would go as far as using same concurency value that flatMap
. Coroutines are cheap, Flow
is designed to provide a powerful map
operator where people will expect parrallel execution for suspending functions.