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

asad.awadia

03/03/2019, 7:25 AM
I can’t come out of reading a channel loop? Its a hard block on the execution?
g

groostav

03/03/2019, 7:53 PM
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

asad.awadia

03/03/2019, 7:54 PM
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

groostav

03/03/2019, 7:58 PM
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

asad.awadia

03/03/2019, 7:58 PM
Is that how you’re supposed to do it though?
I can but that looks unclean and unkotlin like
g

groostav

03/03/2019, 7:59 PM
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

asad.awadia

03/03/2019, 8:01 PM
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

groostav

03/03/2019, 8:08 PM
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

Vsevolod Tolstopyatov [JB]

03/04/2019, 8:49 AM
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

asad.awadia

03/04/2019, 5:59 PM
@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

gildor

03/05/2019, 1:39 AM
In this case channel will leak
a

asad.awadia

03/05/2019, 1:40 AM
If someone writes to it?
What if nothing will write to it?
g

gildor

03/05/2019, 1:40 AM
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
7 Views