Hello there, apart from `delay` what other functio...
# coroutines
a
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
yield, awaitCancellation,suspend[Cancellable]Coroutine, Job.join, Deferred.await, SendChannel.send, ReceiveChannel.receive, Flow.collect
a
Thanks a lot
d
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
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
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
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
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
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
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
"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
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".