deviant
07/12/2017, 6:24 PMthrottle
feature with coroutines
suspend fun <T> ReceiveChannel<T>.consumeThrottled(throttling: Long, action: (T) -> Unit) {
var timestamp = 0L
for (data in this) {
if (timestamp + throttling < System.currentTimeMillis()) {
timestamp = System.currentTimeMillis()
action(data)
}
}
}
is there a proper way?rogeralsing
07/12/2017, 6:30 PMjanvladimirmostert
07/16/2017, 8:44 PMait
07/18/2017, 7:40 PMelizarov
07/18/2017, 8:25 PMlaunch
(fire and forget a new coroutine) or do runBlocking
(to blocking invoking thread while the coroutine is running)galex
07/20/2017, 12:41 PMgroostav
07/20/2017, 9:21 PMrogeralsing
07/20/2017, 11:37 PMrogeralsing
07/23/2017, 2:38 PMelizarov
07/24/2017, 1:50 PMkotlinx.coroutines
version 0.17 is released. It has a new CompletableDeferred
primitive; Job.join
and Deferred.await
are waiting for coroutine’s execution on cancellation; CoroutineScope.context
is renamed to coroutineContext
and more. A full list of changes is here: https://github.com/Kotlin/kotlinx.coroutines/blob/master/CHANGES.mdrafal
07/24/2017, 2:03 PMdelay
in tests?rogeralsing
07/24/2017, 2:30 PMkotlinx.coroutines.experimental.TimeoutException
really be private? I can catch CancellationException which is the base, but still?omarkj
07/24/2017, 8:28 PMferoz_baig
07/26/2017, 8:11 AMfun main(args: Array<String>) = runBlocking {
val job = launch(CommonPool) {
var nextPrintTime = System.currentTimeMillis()
var i = 0
while (i < 10) { // computation loop
val currentTime = System.currentTimeMillis()
if (currentTime >= nextPrintTime) {
println("I'm sleeping ${i++} ...")
nextPrintTime += 500L
}
}
}
delay(1300L) // delay a bit
println("main: I'm tired of waiting!")
job.cancel() // cancels the job
delay(1300L) // delay a bit to see if it was cancelled....
println("main: Now I can quit.")
}
Why is the job not cancelled after calling it explicitly?pilgr
07/26/2017, 12:25 PMrafal
07/26/2017, 4:22 PMgroostav
07/27/2017, 6:26 AMStackTrace
feroz_baig
07/27/2017, 11:04 AMMutex
is slower than ReentrantLock
?rafal
07/27/2017, 12:02 PMchannel.consumeEach {
if(it.sth()){
doSth()
} else {
// stop consuming - break out of consume
}
}
kingsley
07/27/2017, 9:05 PMfuture {}
doesn’t return a deferred. It returns a standard CompleteableFuture
and await
is simply an extension function on top of itrogeralsing
07/27/2017, 9:40 PMvoddan
07/31/2017, 11:57 AMjava.security.AccessControlException: access denied ("java.util.PropertyPermission" "kotlinx.coroutines.debug" "read")
when running code with import kotlinx.coroutines.experimental.channels.*
kevinherron
08/03/2017, 9:22 PMferoz_baig
08/05/2017, 8:14 AMbg
vs Kotlin async
?
Can somebody help me in thiselizarov
08/08/2017, 2:52 PMkotlinx.atomicfu
version 0.1 was released. This is an utility library that enables writing efficient and idiomatic Kotlin code with atomic variables, CAS, etc, without having to write companion objects with j.u.c.a.AtomicXXXFieldUpdater
instances. It is limited for now (only Maven projects are supported), but that is sufficient to use it in implementation of kotlinx.coroutines
(next version will use it). It is currently published to a separate Bintray repository and will soon be synced to JCenter. More details here: https://github.com/Kotlin/kotlinx.atomicfu/blob/master/README.mdelizarov
08/09/2017, 5:20 PMdelay
* If your code is some CPU-heavy stuff, then you must rememeber to check isActive
periodically
* If your code is build with generate
or actor
builder, then you get cancellability “for free” by the virtue of the fact the coroutine’s lifecycle is integrated with lifecycle of the corresponding channel (but you still have to keep in mind CPU-consuming part if you have any of that)kevinherron
08/10/2017, 12:40 AMelizarov
08/10/2017, 7:38 AMkotlinx.coroutines
library, then its async
function returns a Deferred
object that represent the future result of this asynchronous operation: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/async.htmlmikehearn
08/10/2017, 2:36 PMaaverin
08/10/2017, 2:46 PMfun getData() = async(CommonPool) {
//do some network
async(CommonPool) { //do some more parallel network }
.await() //and process results all together
}
Let’s say 3-5 of such written services are called at the same time in parallel by users.
Could this lead to any problems?aaverin
08/10/2017, 2:46 PMfun getData() = async(CommonPool) {
//do some network
async(CommonPool) { //do some more parallel network }
.await() //and process results all together
}
Let’s say 3-5 of such written services are called at the same time in parallel by users.
Could this lead to any problems?kingsley
08/10/2017, 2:50 PMCommonPool
isn’t particularly suited for long blocking IO operations.
Also, since it is (single?) and blocking, it’s clear why time outs will happen more often