https://kotlinlang.org logo
Docs
Join the conversationJoin Slack
Channels
100daysofcode
100daysofkotlin
100daysofkotlin-2021
advent-of-code
aem
ai
alexa
algeria
algolialibraries
amsterdam
android
android-architecture
android-databinding
android-studio
androidgithubprojects
androidthings
androidx
androidx-xprocessing
anime
anko
announcements
apollo-kotlin
appintro
arabic
argentina
arkenv
arksemdevteam
armenia
arrow
arrow-contributors
arrow-meta
ass
atlanta
atm17
atrium
austin
australia
austria
awesome-kotlin
ballast
bangladesh
barcelona
bayarea
bazel
beepiz-libraries
belgium
berlin
big-data
books
boston
brazil
brikk
budapest
build
build-tools
bulgaria
bydgoszcz
cambodia
canada
carrat
carrat-dev
carrat-feed
chicago
chile
china
chucker
cincinnati-user-group
cli
clikt
cloudfoundry
cn
cobalt
code-coverage
codeforces
codemash-precompiler
codereview
codingame
codingconventions
coimbatore
collaborations
colombia
colorado
communities
competitive-programming
competitivecoding
compiler
compose
compose-android
compose-desktop
compose-hiring
compose-ios
compose-mp
compose-ui-showcase
compose-wear
compose-web
connect-audit-events
corda
cork
coroutines
couchbase
coursera
croatia
cryptography
cscenter-course-2016
cucumber-bdd
cyprus
czech
dagger
data2viz
databinding
datascience
dckotlin
debugging
decompose
decouple
denmark
deprecated
detekt
detekt-hint
dev-core
dfw
docs-revamped
dokka
domain-driven-design
doodle
dsl
dublin
dutch
eap
eclipse
ecuador
edinburgh
education
effective-kotlin
effectivekotlin
emacs
embedded-kotlin
estatik
event21-community-content
events
exposed
failgood
fb-internal-demo
feed
firebase
flow
fluid-libraries
forkhandles
forum
fosdem
fp-in-kotlin
framework-elide
freenode
french
fritz2
fuchsia
functional
funktionale
gamedev
ge-kotlin
general-advice
georgia
geospatial
german-lang
getting-started
github-workflows-kt
glance
godot-kotlin
google-io
gradle
graphic
graphkool
graphql
graphql-kotlin
graviton-browser
greece
grpc
gsoc
gui
hackathons
hacktoberfest
hamburg
hamkrest
helios
helsinki
hexagon
hibernate
hikari-cp
hire-me
hiring
hongkong
hoplite
http4k
hungary
hyderabad
image-processing
india
indonesia
inkremental
intellij
intellij-plugins
intellij-tricks
internships
introduce-yourself
io
ios
iran
israel
istanbulcoders
italian
jackson-kotlin
jadx
japanese
jasync-sql
java-to-kotlin-refactoring
javadevelopers
javafx
javalin
javascript
jdbi
jhipster-kotlin
jobsworldwide
jpa
jshdq
juul-libraries
jvm-ir-backend-feedback
jxadapter
k2-early-adopters
kaal
kafka
kakao
kalasim
kapt
karachi
karg
karlsruhe
kash_shell
kaskade
kbuild
kdbc
kgen-doc-tools
kgraphql
kinta
klaxon
klock
kloudformation
kmdc
kmm-español
kmongo
knbt
knote
koalaql
koans
kobalt
kobweb
kodein
kodex
kohesive
koin
koin-dev
komapper
kondor-json
kong
kontent
kontributors
korau
korean
korge
korim
korio
korlibs
korte
kotest
kotest-contributors
kotless
kotlick
kotlin-asia
kotlin-beam
kotlin-by-example
kotlin-csv
kotlin-data-storage
kotlin-foundation
kotlin-fuel
kotlin-in-action
kotlin-inject
kotlin-latam
kotlin-logging
kotlin-multiplatform-contest
kotlin-mumbai
kotlin-native
kotlin-pakistan
kotlin-plugin
kotlin-pune
kotlin-roadmap
kotlin-samples
kotlin-sap
kotlin-serbia
kotlin-spark
kotlin-szeged
kotlin-website
kotlinacademy
kotlinbot
kotlinconf
kotlindl
kotlinforbeginners
kotlingforbeginners
kotlinlondon
kotlinmad
kotlinprogrammers
kotlinsu
kotlintest
kotlintest-devs
kotlintlv
kotlinultimatechallenge
kotlinx-datetime
kotlinx-files
kotlinx-html
kotrix
kotson
kovenant
kprompt
kraph
krawler
kroto-plus
ksp
ktcc
ktfmt
ktlint
ktor
ktp
kubed
kug-leads
kug-torino
kvision
kweb
lambdaworld_cadiz
lanark
language-evolution
language-proposals
latvia
leakcanary
leedskotlinusergroup
lets-have-fun
libgdx
libkgd
library-development
linkeddata
lithuania
london
losangeles
lottie
love
lychee
macedonia
machinelearningbawas
madrid
malaysia
mathematics
meetkotlin
memes
meta
metro-detroit
mexico
miami
micronaut
minnesota
minutest
mirror
mockk
moko
moldova
monsterpuzzle
montreal
moonbean
morocco
motionlayout
mpapt
mu
multiplatform
mumbai
munich
mvikotlin
mvrx
myndocs-oauth2-server
naming
navigation-architecture-component
nepal
new-mexico
new-zealand
newname
nigeria
nodejs
norway
npm-publish
nyc
oceania
ohio-kotlin-users
oldenburg
oolong
opensource
orbit-mvi
osgi
otpisani
package-search
pakistan
panamá
pattern-matching
pbandk
pdx
peru
philippines
phoenix
pinoy
pocketgitclient
polish
popkorn
portugal
practical-functional-programming
proguard
prozis-android-backup
pyhsikal
python
python-contributors
quasar
random
re
react
reaktive
realm
realworldkotlin
reductor
reduks
redux
redux-kotlin
refactoring-to-kotlin
reflect
refreshversions
reports
result
rethink
revolver
rhein-main
rocksdb
romania
room
rpi-pico
rsocket
russian
russian_feed
russian-kotlinasfirst
rx
rxjava
san-diego
science
scotland
scrcast
scrimage
script
scripting
seattle
serialization
server
sg-user-group
singapore
skia-wasm-interop-temp
skrape-it
slovak
snake
sofl-user-group
southafrica
spacemacs
spain
spanish
speaking
spek
spin
splitties
spotify-mobius
spring
spring-security
squarelibraries
stackoverflow
stacks
stayhungrystayfoolish
stdlib
stlouis
strife-discord-lib
strikt
students
stuttgart
sudan
swagger-gradle-codegen
swarm
sweden
swing
swiss-user-group
switzerland
talking-kotlin
tallinn
tampa
teamcity
tegal
tempe
tensorflow
terminal
test
testing
testtestest
texas
tgbotapi
thailand
tornadofx
touchlab-tools
training
tricity-kotlin-user-group
trójmiasto
truth
tunisia
turkey
turkiye
twitter-feed
uae
udacityindia
uk
ukrainian
uniflow
unkonf
uruguay
utah
uuid
vancouver
vankotlin
vertx
videos
vienna
vietnam
vim
vkug
vuejs
web-mpp
webassembly
webrtc
wimix_sentry
wwdc
zircon
Powered by Linen
coroutines
  • g

    gildor

    09/21/2018, 10:43 AM
    I think it shouldn’t be “migration guide”, but rather “learn coroutines on Rx examples”
    👍 2
    ☝️ 1
    t
    1 reply · 2 participants
  • m

    muralimohan962

    09/21/2018, 11:47 AM
    Recently I was wondering why the default launch coroutine builder is deprecated. Why did they do it?
    m
    3 replies · 2 participants
  • z

    Zach Klippenstein (he/him) [MOD]

    09/21/2018, 1:06 PM
    Might be jumping the gun here because the
    api-review
    branch hasn't been merged yet, but i noticed this commit deprecates the
    onCompletion
    parameters to launch, async, etc. What's the best way to wire up channels to cancel on coroutine completion after this change? Explicit try/catch in the coroutine? (Is there a discussion about this change anywhere I could read up on?) https://github.com/Kotlin/kotlinx.coroutines/commit/3049652ff5e3a23e35bcc82e28fb87936ed836ec
    v
    6 replies · 2 participants
  • i

    igorvd

    09/21/2018, 1:45 PM
    someone have an guide showing how to migrate from coroutines 0.25.0 to 0.26.1 ? I understand that have some big changes, but things that usually worked before, aren't working now. I'm trying to get a result from a suspend function, inside another suspend function, without wrapping in any new coroutine, and it's not working... If I debug, I can see the right result in the
    return
    line on my second suspend function, but it never return to the first one...
    n
    2 replies · 2 participants
  • j

    jw

    09/21/2018, 8:59 PM
    in what repo is atomicfu 0.11.7-eap13? it's not in maven central or the eap repo. nor dev nor kotlinx...
    e
    3 replies · 2 participants
  • s

    spierce7

    09/22/2018, 3:58 AM
    @elizarov Has the coroutine library been released in a format that can be pulled in with the new Kotlin multiplatform gradle plugin? https://github.com/h0tk3y/k-new-mpp-samples I'm trying to pull 0.26.1 manually for each source-set indepdendently but encountering gradle sync fails without good error outputs.
    e
    1 reply · 2 participants
  • l

    littlelightcz

    09/22/2018, 9:49 AM
    Hi there, I am just getting my lib ready for the coming of 1.3 (I am using 1.3.0-rc-57), and just a little naming note - when I look into the Dispatchers object, there is Default and Unconfined. I wouldn't mind going lowercase here, since it's just a field, right? (not a subclass etc.) I remember I already proposed going with lowercased
    io {}
    pool as well (in some github discussions), so I am just re-throwing this idea again before the release decisions will be final (if they're not already) 🙂 .
    e
    6 replies · 2 participants
  • n

    Nikky

    09/22/2018, 10:27 PM
    can i name coroutines ? so i see job-abc or w/e instead of coroutine#1234 when i enable debugging
    ➕ 1
    g
    v
    2 replies · 3 participants
  • n

    Nikky

    09/23/2018, 12:27 AM
    also something else thats weird, i am in a screenshare with friend, they have the issue that some coroutines work fine locally but break down once executed on their debian server, coroutines do not wait on the scope, they seem to just all start, and then the program closes they are on openjdk 1.8.0-171 on debian
    e
    6 replies · 2 participants
  • v

    vaskir

    09/23/2018, 10:39 AM
    coroutineScope {
        (1..30)
            .map {
                async {
                    print("offering $it...")
                    queue.offer(it).await()
                    println("offered $it")
                }
            }.awaitAll()
    }
    e
    1 reply · 2 participants
  • n

    Nikky

    09/23/2018, 6:47 PM
    it seems to have been something in ktor-client-okHttp taking its time to shut down which holds back the main function from closing
    b
    g
    6 replies · 3 participants
  • g

    gotoOla

    09/23/2018, 6:53 PM
    When I read the article I’m thinking the the implementation of CoroutineScope is more important when you do things such as Activitys in android etc. Our microservices are strictly backend and the dependency context of the application is created once during start so we don’t typically do “new MyService()”
    e
    3 replies · 2 participants
  • o

    otakusenpai

    09/24/2018, 5:12 AM
    whats the current version of kotlin coroutines and how do i include it in my project ?
    e
    4 replies · 2 participants
  • d

    Dominaezzz

    09/24/2018, 1:08 PM
    Out of curiosity, why isn't
    runBlocking
    in the standard library? Since you can suspend without kotlinx.coroutines, shouldn't you be able to at least block without it?
    s
    u
    +1
    6 replies · 4 participants
  • g

    gildor

    09/24/2018, 2:44 PM
    To test code in doStuff just test this function, do not test myMethod
    👍 3
    🤔 1
    💯 2
    a
    23 replies · 2 participants
  • s

    spand

    09/25/2018, 12:17 PM
    Trying to migrate to 1.3.0-rc-57 and 0.26.1-eap13 and I get errors for code that is basically:
    synchronized(lock){
        implicitScope.launch {
            delay(1)
        }
    }
    I get an error that says: " The 'delay' suspension point is inside a critical section". Seems safe to me ?
    v
    u
    13 replies · 3 participants
  • s

    spand

    09/25/2018, 12:23 PM
    The same code when compiling in idea breaks with internal error mentioning
    SuccessOrFailure
    . I thought that had been renamed?
    v
    1 reply · 2 participants
  • d

    darych

    09/25/2018, 3:10 PM
    Hi! I'm newbie in coroutines. I'm wondering how I can make Spring's RestTemplate work with coroutines? I'm going to check ktor's source code. But from my perspective, network intercation in RestTemplate is blocking and there will be no improvement with coroutines. Am I right?
    e
    k
    7 replies · 3 participants
  • r

    redrield

    09/25/2018, 7:07 PM
    is there a way to reset their state such that they can run again?
    t
    1 reply · 2 participants
  • k

    kirillrakhman

    09/26/2018, 12:46 PM
    I'm just migrating to new scoped coroutines. In one case, I'm inside a
    Fragment
    which implements
    CoroutineScope
    . The default cancellation behavior is to cancel in
    onDestroy
    . But one specific coroutine I want to cancel on a different event, e.g. in
    onDetach
    . Should I create a nested scope or should I just manually cancel the coroutine when the event happens?
    k
    g
    5 replies · 3 participants
  • e

    elizarov

    09/26/2018, 8:56 PM
    📣 📣 📣
    kotlinx-coroutines
    version
    0.27.0
    is published — polishing structured concurrency, getting ready for release, fixing bugs, see for details https://github.com/Kotlin/kotlinx.coroutines/releases/tag/0.27.0
    🎉 15
    1 reply · 1 participant
  • n

    nwh

    09/26/2018, 9:15 PM
    Is there a way to debug a coroutine's scope tree?
    e
    3 replies · 2 participants
  • e

    enleur

    09/26/2018, 9:16 PM
    I see
    currentScope
    is deprecated now. How should we run launch inside
    suspend fun
    now? Something like this
    CoroutineScope(coroutineContext).launch {  }
    ?
    v
    e
    2 replies · 3 participants
  • r

    rdhruva

    09/26/2018, 9:54 PM
    upgrading my android project to coroutines 0.27.0 pegs android studio 3.3 canary 12 to 300x CPU utilization.. is it a known issue?
    l
    9 replies · 2 participants
  • l

    louiscad

    09/26/2018, 9:54 PM
    Now that
    currentScope { }
    is deprecated, should I use its implementation,
    CoroutineScope(coroutineContext)
    instead? I have some code relying on it:
    suspend inline fun <E> ReceiveChannel<E>.doOnEach(
            context: CoroutineContext = EmptyCoroutineContext,
            noinline action: suspend (E) -> Unit
    ) = currentScope {
        launch(context) { consumeEach { action(it) } }
    }
    that would become:
    suspend inline fun <E> ReceiveChannel<E>.doOnEach(
            context: CoroutineContext = EmptyCoroutineContext,
            noinline action: suspend (E) -> Unit
    ) = CoroutineScope(coroutineContext).launch(context) { consumeEach { action(it) } }
    e
    3 replies · 2 participants
  • g

    gildor

    09/27/2018, 5:30 AM
    You cannot, because this function already have another receiver This is one of the main reasons why we need extension function with multiple receivers, please vote and add your use cases https://youtrack.jetbrains.com/issue/KT-10468
    e
    1 reply · 2 participants
  • g

    gildor

    09/27/2018, 5:32 AM
    We have exactly the same function on own CoroutineScope implementation that we used before structure concurrency release, but we add this extension function as member of our custom CoroutineScope
    e
    1 reply · 2 participants
  • m

    Martin Devillers

    09/27/2018, 8:19 AM
    I often find myself having to load a global resource asynchronously, where it only needs to be loaded once, but it could potentially fail. An example is the delta with the server time, so that I can implement an NTP. For those cases, I implemented a
    DeferredCache
    with an
    await
    method which when called: - if no coroutine to load the resource has been launched, it starts a new one - if a coroutine to load the resource has finished successfully, it returns its result - if a coroutine to load the the resource has finished with an error, it restarts a new one - if a coroutine to load the resource has been launched and is still active, it awaits its result My current implementation looks like this
    class DeferredCache<T: Any>(
        private val coroutineScope: CoroutineScope = GlobalScope,
        private val block: suspend CoroutineScope.() -> T
    ) {
        private var deferredValue: Deferred<T>? = null
        private val deferredValueLock = Mutex()
    
        private val usableDeferredValue: Deferred<T>?
            get() = deferredValue?.takeUnless { it.isCompletedExceptionally }
    
        suspend fun await(): T {
            val deferredValue = usableDeferredValue ?: deferredValueLock.withLock {
                usableDeferredValue ?: coroutineScope.async(NonCancellable, block = block).also { deferredValue = it }
            }
            return deferredValue.await()
        }
    }
    Can you think of a more optimal solution? Anyone running into this scenario as well? Might it be worth adding a “standard” way to do this?
    s
    c
    +1
    21 replies · 4 participants
  • a

    aaverin

    09/27/2018, 1:37 PM
    With coroutines-android, when logging coroutine scope, is it possible to tell the difference between Android.Main dispatched coroutine and non-main dispatched coroutine?
    g
    w
    4 replies · 3 participants
  • n

    nerses

    09/27/2018, 2:51 PM
    hi, i have a question regarding new structured coroutines. In here it says https://github.com/Kotlin/kotlinx.coroutines/blob/master/docs/basics.md#scope-builder
    The main difference between runBlocking and coroutineScope is that the latter does not block the current thread while waiting for all children to complete.
    But when i run the example there the nested launch inside
    coroutineScope
    blocks the execution of the
    runblocking
    scope and the last
    println("Coroutine scope is over")
    is always printed the last. Am i getting it wrong? Or there is a mistake in the docs
    k
    3 replies · 2 participants
Powered by Linen
Title
n

nerses

09/27/2018, 2:51 PM
hi, i have a question regarding new structured coroutines. In here it says https://github.com/Kotlin/kotlinx.coroutines/blob/master/docs/basics.md#scope-builder
The main difference between runBlocking and coroutineScope is that the latter does not block the current thread while waiting for all children to complete.
But when i run the example there the nested launch inside
coroutineScope
blocks the execution of the
runblocking
scope and the last
println("Coroutine scope is over")
is always printed the last. Am i getting it wrong? Or there is a mistake in the docs
k

kingsley

09/27/2018, 2:55 PM
coroutineScope
exits only after all the scoped coroutines have completed. However, it doesn’t block any thread. Once execution is suspended in the
coroutineScope
block, the thread is free to do some other work until the block is resumed again continuing from where it left off
Did you read this yet? https://medium.com/@elizarov/structured-concurrency-722d765aa952
n

nerses

09/27/2018, 3:53 PM
ah ok, thanks @kingsley after rereading that blog post it makes sense now 🙂
👍 1
View count: 2