https://kotlinlang.org logo
#coroutines
Title
# coroutines
a

aleksey.tomin

11/27/2020, 7:23 AM
How can I investigate a problem “`kotlinx.coroutines.delay(100)` sometimes works 10 seconds”? Some breakpoint?
b

bezrukov

11/27/2020, 9:08 AM
it doesn't do any work, it simply re-schedule coroutine with specified delay. So there are some factors affecting real delay 1. Dispatcher may be busy so you can review all running coroutines (Probably via DebugProbes.dumpCoroutines) 2. CPU sleep affects delays, so you have to determine whether it's sleeping or not 3. Same as 1. But one level deeper (assuming you're on JVM) - there are a lot of running threads (CPU-consuming), so your dispatcher doesn't receive enough cpu-credits
a

aleksey.tomin

11/27/2020, 10:46 AM
I’ve added
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-debug:1.4.1-native-mt")
but I have got an error
/app.kt: (33, 40): Unresolved reference: debug
on
Copy code
kotlinx.coroutines.debug.DebugProbes.dumpCoroutines()
@bezrukov What I do wrong?
s

Saul Wiggin

11/27/2020, 11:07 AM
Should variable be a Long? 100L. This works for me for 10s
Copy code
GlobalScope.launch { delay(10000L) }
a

aleksey.tomin

11/27/2020, 11:14 AM
@Saul Wiggin
delay(3.seconds)
Copy code
13'th done in 3.004999936s average=3.001999990153846s, max=3.004999936s
14'th done in 3.003000064s average=3.002071424s, max=3.004999936s
15'th done in 12.999000064s average=3.6685333333333334s, max=12.999000064s
16'th done in 13.0s average=4.25175s, max=13.0s
17'th done in 13.0s average=4.7663529411764705s, max=13.0s
u

ursus

11/28/2020, 4:18 AM
If I might OT, how would one make something time delay related but is mission critical to be precise? From what I gather high level threads cannot guarantee anything since its all up to cpu scheduler's time sharing right? Is the only solution something like a dedicated chip?
a

aleksey.tomin

11/28/2020, 7:53 AM
I use ktor-client. It works with delay for prevent blocking.
u

ursus

11/28/2020, 8:39 PM
Hm? My question was general smart phone cpu I belive
b

bezrukov

11/28/2020, 9:25 PM
Usually you have to work with system's power manager like Android's/Linux's wakelock to schedule job that need's to be done at exact time
u

ursus

11/30/2020, 11:09 PM
But that wont be exact for real, say for some self driving car stuff, since all cores can be occupied and then time sharing comes into place, no?
a

aleksey.tomin

12/01/2020, 3:59 AM
I’ve had the problem on macOS and it’s macOS-specific problem.
u

ursus

12/01/2020, 2:23 PM
I know, the battery saver thing, but I meant as OT, that I believe this is a general purpose computer issue, that time keeping is inexact really, nevermind
17 Views