I can’t come out of reading a channel loop? Its a ...
# coroutines
a
I can’t come out of reading a channel loop? Its a hard block on the execution?
g
I cant come out of a channel loop
meaning a for-each loop on a receive channel? Yeah, it only exits when the channel has completed. You might consider using a
withTimeoutOrNull
+
receieve
or
select { channel.onReceieve {} ...
instead of a for-each loop if you need more control
Its a hard block on the execution
Certainly not, the caller is suspended, and there's no execution in a channel per-se. Remember most of the time you wire a producer up to your channel that you're then consuming. When the consumer attempts to read an element it does so fully asynchronous concurrently with the producer
a
How do i get to the next line from the ‘read from a channel’ without sending a close?
Lets say theres a bunch of coroutines sending a string to a channel - can i stop the infinite loop over reading the channel? And get to the next line without sending close - because I cant guarantee that the close will be sent after all the values have been sent
g
I'm not sure what you mean by "next line", but cant you detect your cercomstance and
break
?
Copy code
for(element in foreverChannel){
  if(state + element > threshold){
    break;
  }
}
?
a
Is that how you’re supposed to do it though?
I can but that looks unclean and unkotlin like
g
so, you do have to ask about cancellation, if the consumer doesnt ever consume any more elements from
foreverChannel
, do you want it to cancel that channel?
if that is true, then
consumeEach
is what you want, and if thats true you can use the standard functional
first
combinator
ie
Copy code
val result = foreverChannel.first { state + it > threshold }
that will cause
foreverChannel
to be cancelled after the first element that matches that criteria, which causes your producers to all get signalled next time they try to produce
a
Hmm 🤔
Surprised kotlin doesn’t already have syntactic sugar for this
In go you can create a buffered channel and then read that many elements in a loop
I guess I could do that
g
I think its a problem only if you dont have reasonable assurances that there are producers and consumers --as long as there will be another consumer that's taking elements n+1 until the close signal. But I just had this same thought: what I'd really like is a
take(n)
that doesn't cancel the channel after receiving those elements.
quick point, you cant
close
a
ReceiveChannel
, you can only
cancel
it. Producers
close
the `SendChannel`s.
If you think about your program as being a kind of parser then trying to look ahead on a channel some number of tokens (messages) without cancelling or closing the channel is totally reasonable.
v
We don’t have a built-in mechanism that loops
n
times and then returns without cancelling a channel (otherwise, you can use
take
). If you think this is a frequent use-case that keeps popping here and there, feel free to create an issue on github.
in you project you can just create a specific extension:
Copy code
suspend inline fun <E> ReceiveChannel<E>.iterate(limit: Int, action: suspend (element: E) -> Unit) {
        var consumed = 0
        for (element in this) {
            action(element)
            if (++consumed == limit) {
                break
            }
        }
    }
a
@Vsevolod Tolstopyatov [JB] thank you. Much appreciated.
I read Roman’s answer on stack Overflow that not closing a channel is nbd - that’s still applies?
Thats my biggest concern - reading from a channel and then just moving on without closing it
g
In this case channel will leak
a
If someone writes to it?
What if nothing will write to it?
g
if this channel blocks some coroutine (like in case of
produce
)
it depends on channel implementation, it can be garbage collected if there are no more links on this channel
it depends on implementation, for example:
Copy code
val channel = produce { 
   while(true) {
      send("something")
   }
}
if you never close this channel it will leak coroutine and channel, but it’s not so bad, because now you can call produce only in CoroutineScope, this channel will be closed together with scope