https://kotlinlang.org logo
#coroutines
Title
# coroutines
l

Lulu

11/17/2020, 3:32 PM
I have a
Flow
of requests that need to be processed by (many) different workers, and I was wondering which option is better from the following: 1. Have each worker apply
filter {  }
on the main
Flow
to check if this request belongs to it. 2. Have a separate
Flow
for each worker, and a general worker that checks all the main `Flow`'s requests and re-emit each request to the corresponding worker's
Flow
. 3. (possibly something I haven't thought of?)
d

Dominaezzz

11/17/2020, 3:40 PM
Is each request processed by a single worker or by each worker?
l

Lulu

11/17/2020, 3:41 PM
A single worker, and it depends on a parameter in the request. Sorry if that wasn't clear.
s

Shalom Halbert

11/17/2020, 3:57 PM
Would adding the
filter
lambda as a function parameter be suitable?
That would enable each consumer to control the filter, which is more flexible
l

Lulu

11/17/2020, 3:59 PM
That's option 1. I'm just afraid going this direction would make the program less efficient, but I have no clue how flows work internally
a

andylamax

11/17/2020, 5:30 PM
If you have too many request, making all the subscribers filter might be such a good idea. Instead of filtering the request after subscription, why don't you filter the flows during subscription? that way, every collector will subscribe to their respective flows? Yes, it means you are gonna have to have multiple flows
l

Lulu

11/17/2020, 7:19 PM
So basically
filter {  }
then a terminal operation? Is that what you're suggesting each worker do?
a

andylamax

11/17/2020, 7:55 PM
No. say you have Flow<A>, Flow<B>, Flow<C>, then you define a method say,
fun <T> eventFlow(type: T) : Flow<T> = when(type
) { is A -> flow<A>{} is B -> flow<B>{} // . . . . }
l

Lulu

11/17/2020, 7:59 PM
I see, you're for option 2. I think that's what I'll end up using. Thanks!
a

andylamax

11/17/2020, 8:03 PM
No, your option 2, re-emits requests. I am suggesting to filter the flows themselves. Not the requests. Or did I miss something?
l

Lulu

11/17/2020, 8:05 PM
When you have an upstream flow, how do you use
when {  }
to decide which downstream flow it is going to without re-emitting it into that downstream flow? Unless I missed your point.
a

andylamax

11/17/2020, 8:27 PM
that is not an operator. It is just a when expression
4 Views