taer
01/24/2020, 10:54 PMprivate suspend fun poll2() {
while (true) {
consumer.poll(Duration.ofMillis(500))
.forEach {
channel.send("ping")
}
}
}
}
Channel is a kotlin Channel. If consumer.poll returns empty, the foreach doesn't execute. So we don't ever execute any suspendable code, so this loops forever, not giving back the thread to the coroutine. If I add a delay(1)
immediately preceeding the poll, then other coroutines get a chance to run. Is there a cheaper alternative to an arbitrary 1ms delay?
BTW, this is an attempt to wrap a Kafka consumer output into a easier to consume Kotlin channel.octylFractal
01/24/2020, 10:54 PMyield()
?taer
01/24/2020, 10:56 PMZach Klippenstein (he/him) [MOD]
01/24/2020, 10:58 PMsequence { }
builder. Confusing, I know.taer
01/24/2020, 10:59 PMoctylFractal
01/24/2020, 11:28 PMFlow
insteadbdawg.io
01/25/2020, 2:25 AMyield()
is a generic suspend function that can be used on any suspension context. yield(value: T)
is a function definition on the SequenceScope
provided inside of sequence { }
and iterator { }
octylFractal
01/25/2020, 2:26 AMyield()
in sequence {}
taer
01/27/2020, 3:21 PM