https://kotlinlang.org logo
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
benchmarks
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
confetti
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
lincheck
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
Title
a

andylamax

05/22/2022, 6:53 PM
Hello there, apart from
delay
what other functions actually suspends? I am trying to get an example of a function that actually suspends (like delay), Not just marked as a suspend function and goes into IO operations like many I have seen
e

ephemient

05/22/2022, 6:59 PM
yield, awaitCancellation,suspend[Cancellable]Coroutine, Job.join, Deferred.await, SendChannel.send, ReceiveChannel.receive, Flow.collect
a

andylamax

05/23/2022, 11:04 AM
Thanks a lot
d

DALDEI

06/07/2022, 1:56 AM
Unfortunately -- almost nothing else nor anything 'useful' unless you write it yourself -- or find a 3rd party library that implements suspernd functions correctly. this is a core problem with the adoption of coroutines -- most of the theory (and examples) have little actual value unless running in a 'coroutine aware' program. A program that already makes use of / pre-exists suspending functions for 'useful work. In no existing code that has any part of it that pre-dates coroutines does this occur in nature -- so all the docs and use cases that are casually written with pseudo code showing delay(2000) // Simulate 'Acutal work' are examples of things that do not occur 'in the wild' -- i.e. in your program UNLESS you have already implemented them -- and in the JVM -- that is not easy at all -- most of the obvious cases, like file IO , networking, 3rd party libraries , DB calls etc -- dont come with suspend functions or even reasonably easy to convert (like Completable Futurers) -- How many HTTP or DB libraries that your program actually use are 'coroutine friendly' ? OR expose a usable non-blocking interface that can be wangled into a coroutine ? And if they did -- how practical is it to actually properly wrap all the APIs into proper suspend functions? In my experience, nearly zero -- or exactly zero depending on how one defines "practical" --- *This leads to a big adoption problem even in one owns code -- as the real benifits of using coroutines doesnt usualy pay off unless a good chunk of the 'useful work' in your code is a suspend function or similar. i.e -- ones own adoption of coroutines is throttled by the extent already existing of ones own adoption of coroutines --*
a

andylamax

06/07/2022, 8:25 AM
Many (if not all) things I meet in real life are IO bound. file IO, networking e.t.c. So, in most cases, I am just wrapping these IO operations (which are mostly blocking) into suspend functions. And If I recall, a blocking operation will block no matter what, which even brought rise to the IO Dispatcher that actually spawns new thread(s) so that once blockings happen, they don't happen in the thread that spawned the process. Which felt a bit ironic
e

ephemient

06/08/2022, 12:07 AM
non-blocking IO does exist, e.g. java.io.nio.channels.AsynchronousChannel; it's just not as commonly used
and with Project Loom, even blocking java.io calls will be async under the hood
d

DALDEI

06/09/2022, 1:43 PM
Non blocking IO has existed in java for a long time. However 'existing' and 'tolerable to use' are different. Even senior java devs do not casually use NIO, it is a serious PITA and shares few high level interfaces. "What Kotlin Needs" , IMHO , is something like a kotlinx.io library to expose the core NIO APIs as standard suspending apis, and then integrated into all the stdlib classes in the same way File, Input/Output Stream are
👍 1
e

ephemient

06/09/2022, 1:53 PM
for what it's worth, Linux doesn't actually implement async IO on local files https://man7.org/linux/man-pages/man7/aio.7.html
The current Linux POSIX AIO implementation is provided in user space by glibc. This has a number of limitations, most notably that maintaining multiple threads to perform I/O operations is expensive and scales poorly.
so the only things worth special integration into kotlinx.coroutines are network streams and the like (IMO, because Linux servers and Android devices are by far the dominant use cases)
d

DALDEI

06/09/2022, 10:56 PM
Interesting note on the AIO. I disagree that makes it not 'worth special implementation' The goal being a consistant suspending interface to standard IO objects Whether or not it is ideally implemented, is more performant, is implemented using native kernel async IO or userspace threads. Having a standard way to interface with coroutines has much value
In the same sense that having the same Collection & Iterator functions that work the same across all collection types, sequences, flows, and other things like Strings -- Even if for any given method there may be a more efficient way that is specific to that instance Having the same set of operations across all collection-like objects has great value. Using suspend functions for 'blocking IO' is not (only) about performance -- often its about deterministic behaviour. There are cases where its more important to guarentee that all calls are non-blocking -- independant of if they are effecient -- the android "Main UI Thread" being one. Web servers, microservices, database internals, protocol adaptors, -- many use cases.
e

ephemient

06/10/2022, 3:05 AM
let me clarify myself. for file I/O,
withContext(<http://Dispatchers.IO|Dispatchers.IO>) { ... }
is about as good as you're going to get on Linux, no matter how you dress it up. you have to choose some dispatcher or executor for it, and even if you hide it behind another abstraction, you still have to be aware of its limits. (there is some potential in io_uring, but figuring out the ownership to bridge it to non-native code is not trivial, and for file I/O it simply spawns a kernel thread so you're still limited by that, just with a different constant) socket I/O, on the other hand, has a lot more room where integration with coroutines could lead to better results. if it's built on NIO.2, then it'll happen to work for files too. but you shouldn't intentionally use it for files because the underlying implementation will use an unbounded executor that doesn't handle cancellation
d

DALDEI

06/27/2022, 4:26 AM
"even if you hide it behind another abstraction, you still have to be aware of its limits." Absolutely. A (good?) analogy is the work done for kotlin flows. That clearly took a LOT of thought and precision in convention as well as implementation to maintain a consistant paradigm with understandable well defined behaviour across the entire use cases Not easy. Which is why I suggest its a very useful thing to do .. *by some one/team who is very good at it * if its (design of standard suspending IO library) done ad-hoc by many, or less then expertly by few -- the result is arguably worse then not at all - so I do understand why such a thing is not part of the standard lib nor kotlinx at this point, its definately non-trivial. But would it be useful if implemented well ? hard to prove upfront. My oppinion is that it would be, but hard to qualify or measure. (how do you measure usefulness?)
e

ephemient

06/28/2022, 1:51 AM
Flows are a great abstraction but cannot do everything. for example, while you could expose a network stream as a
Flow<Byte>
, you should not as there is simply too much overhead that is necessary for other general purposes. similarly, scalable async IO for files is simply impossible from the level at which Kotlin operates. sure, if you made it ergonomic to use NIO.2 channels in suspend, that would be great for network IO, but that doesn't get around the platform issues with file IO, even though it would technically "work".