Hi! There is 'select' clause for channels that is ...
# coroutines
p
Hi! There is 'select' clause for channels that is one time shot. Is there any solution to make a flow from select? There is a need to continuously consume elements from any channel in select. Building select again after each consumption seems as slow solution.
l
Seems, but is it?
p
Registering,unregistering all channels on each iteration (in my case there is around 100) is obvious overhead.
l
Why do you have 100 channels? What kind of thing is it, and what is the purpose of an iteration, in your case?
u
You can convert your channels to flows (
fun <T> ReceiveChannel<T>.consumeAsFlow(): Flow<T>)
and then merge the flows (if I got your need right). If you do not want to merge, why not start 100 coroutines, each consuming one channel?
And +1 for @louiscad question. Is it slow? Even with 100 channels you might be surprised. Remember Donald Knuth. “PrematureOptimization can be defined (in less loaded terms) as optimizing before we know that we need to.”
p
I'm thinking of best way for handling events from many channels. I have iot server in which each device is represented as a channel from which server can get device statuses. Each device can configure channel on creation (blocked, conflated, etc).
u
You can convert your channels to flows
So what do you think?
p
Seems what I need. Will check.
z
If you’re using the channels to process actual network IO, then that’s probably your long poll anyway and the performance of select insignificant. IO is generally at least an order of magnitude slower than pure computation.