mayojava
10/03/2018, 7:00 AMasync
should get cancelled after the exception since they are in the same scope.Felix
10/03/2018, 5:01 PMasync
, obtaining Deferred
objects
- Coroutine then cancels one of those Deferred
objects.
1) How can such a cancel
trigger a cancellation on the pending HTTP request? The Apache Async Client has a way to cancel requests, however since there isn't any thread actively monitoring the isActive
, I need to "link" the Deferred
cancel with the pending request cancel. For instance, in .NET the CancellationToken
as a Register
method to register an action to be performed.
2) Also, if the coroutine decides to ignore the cancellation (e.g. it is to late to cancel the HTTP request), how can we observe that on the Deferred
? What I'm observing is that the await
will throw the JobCancellationException
even if the cancel
is ignored.nimtiazm
10/04/2018, 5:13 AMnimtiazm
10/04/2018, 5:15 AMelizarov
10/04/2018, 11:20 AM0.30.1
for the people still on experimental (Kotlin 1.2) coroutines, and 0.30.1-eap13
for people who are playing with RC of Kotlin 1.3 where coroutines are no longer experimentalPaul Woitaschek
10/04/2018, 2:28 PMuhe
10/04/2018, 2:54 PMdave08
10/04/2018, 2:55 PMmuralimohan962
10/05/2018, 4:47 AMAlbert
10/05/2018, 8:14 AMwithContext
?
withContext(GlobalScope.coroutineContext) {
async {
}
launch {
}
}
Konstantin Petrukhnov
10/05/2018, 11:51 AMkirillrakhman
10/05/2018, 11:52 AMAlbert
10/05/2018, 12:09 PMcurrentScope
is deprecated. For example:
suspend fun httpGetAsync(): Deferred<String> = currentScope {
async {
// ... Do HTTP GET
}
}
suspend fun main() {
coroutineScope {
val request1 = httpGetAsync()
val request2 = httpGetAsync()
println("${request1.await()} - ${request2.await()")
}
}
If httpGetAsync
was using coroutineScope
, it means that request2
will be filled when request1
is completely done.voddan
10/05/2018, 3:31 PMnwh
10/07/2018, 12:38 AMHadi Tok
10/07/2018, 2:04 PMprint(5)
to run. how should I go for doing this? the current code I sent runs the coroutines in loop parallel but print(5)
does not wait for completion of the others.redrield
10/07/2018, 4:44 PMelizarov
10/07/2018, 6:36 PMsequence { }
. It is truly cold. But it is synchronous. We don’t have async cold streams yet.nwh
10/07/2018, 6:43 PMsuspend
function? If I don't use launch/async
etc to launch anythingTwoClocks
10/07/2018, 6:59 PMfun main(args : Array<String>) {
var taskHandle:Continuation<Unit>?=null
runBlocking { // If I run in this context, this block never exits.
launch {
while(true) {
println("about to block")
val retVal = suspendCoroutine<Unit> {
taskHandle = it;
}
println("done blocking.. on thread : ${Thread.currentThread().name}")
}
}
println("done launching")
}
println("all coroutines blocked, do work...")
repeat( 10, {
Thread.sleep(ThreadLocalRandom.current().nextInt(2000) + 500L)
taskHandle!!.resume(Unit)
})
}
sarel
10/08/2018, 2:51 AMRohit Surwase
10/08/2018, 5:29 AM// Downloaded 1.3. early preview plugin of Kotlin for Android Studio
ext.kotlin_version = '1.3.0-rc-46'
// ext.kotlin_version = '1.3.0-rc-57'
maven { url '<https://dl.bintray.com/kotlin/kotlin-eap>' }
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
implementation ("org.jetbrains.kotlinx:kotlinx-coroutines-core:0.30.1") {
exclude group: "org.jetbrains.kotlinx", module: "atomicfu-common"
}
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:0.30.1'
Above setup giving me error No route to host (connect failed)
3. What is working for me is just
ext.kotlin_version = '1.2.71'
implementation ("org.jetbrains.kotlinx:kotlinx-coroutines-core:0.30.1") {
exclude group: "org.jetbrains.kotlinx", module: "atomicfu-common"
}
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:0.30.1'
Please guide me in the right direction. Thanks.kenkyee
10/08/2018, 5:44 AMVsevolod Tolstopyatov [JB]
10/08/2018, 4:14 PMkotlinx.coroutines
version 0.30.2
.
Release contains fixes for issues with Dispatchers.Main
and various improvements.
Full changelog: https://github.com/Kotlin/kotlinx.coroutines/releases/tag/0.30.2gildor
10/09/2018, 5:20 AMwe do not need to create our own threads in Android for any reason?@Rohit Surwase I don’t see any common reason even without coroutines create own threads on Android if you not a library author (usually you use ThreadPoolExecutor or some high level primitives) All those cases (1-3) are covered easily by coroutines and provide much better API without drawbacks. Especially if 1 and 2 have async api
gildor
10/09/2018, 5:21 AMBut as standalone coroutines are deprecated what other alternatives do we have apart from GlobalScopeWhat do you mean? GlobalScope is direct successor of old standalone coroutine builders and provides the same lifecycle. But usually you don’t need it, use CoroutineScope everywhere where it’s possible to do
has examples aboutIt’s just for simplicity of examples, docs has actually a lot of different examples, not only runBlocking or GlobalScopeandrunBlocking{ }
onlyGlobalScope.launch( )
marcinmoskala
10/09/2018, 3:07 PMjw
10/09/2018, 3:11 PMpoohbar
10/09/2018, 6:46 PMparallelStream
equivalent in kotlin.. does this make sense?
runBlocking {
myItems.map {
GlobalScope.launch {
// do something
}
}.forEach { it.join() }
}
// now do something with all items
nwh
10/09/2018, 8:58 PMctx
to the launch
method redundant in this scenario?
val ctx = newSingleThreadContext("sample")
val scope = CoroutineScope(ctx)
scope.launch(ctx) { }
nwh
10/09/2018, 8:58 PMctx
to the launch
method redundant in this scenario?
val ctx = newSingleThreadContext("sample")
val scope = CoroutineScope(ctx)
scope.launch(ctx) { }
louiscad
10/09/2018, 10:02 PMvoddan
10/10/2018, 8:35 AMlaunch
accepts an optional context then? Can't the API be minimized to always use the scopes?louiscad
10/10/2018, 8:44 AMCoroutineContext
is folded with the context from the scope. That gives you structured concurrency, but you can still launch in another dispatcher, or add something to the scope's context.