Pacane
08/10/2020, 6:42 PM// This is in a loop
val e = async {
writeFileToDisk()
}
filesBeingWritten.add(e)
//somewhere else
filesBeingWritten.awaitAll()
Is much slower than this
// This is in a loop
writeFileToDisk()
where writeFileToDisk()
is a suspending function calling
withContext(<http://Dispatchers.IO|Dispatchers.IO>) {
val get = Paths.get(imageFilename)
Files.write(get, bytes)
}
And by "slower" I mean it never seems to even complete..? Is there a limitation / maximum amount of Deferred objects we can await at a same time?octylFractal
08/10/2020, 6:46 PMasync
and where the awaitAll()
is, you might be causing some unexpected issuesoctylFractal
08/10/2020, 6:47 PMPacane
08/10/2020, 6:48 PMprivate suspend fun closeMotion(motionDirectory: Path) = coroutineScope {
// Some code omitted
val begin = Instant.now()
val f1 = listOf(*filesBeingWritten.toTypedArray())
println("Awaiting ${f1.count()} frames to be written.")
filesBeingWritten.clear()
try {
f1.awaitAll()
} catch (e: Exception) {
println("AAAA $e")
}
println("It took : ${Duration.between(begin, Instant.now()).seconds}")
}
Pacane
08/10/2020, 6:48 PMawaitAll()
Pacane
08/10/2020, 6:49 PMPacane
08/10/2020, 6:49 PMfilesBeingWritten
object is just a mutableList of Deferred<Unit>
octylFractal
08/10/2020, 6:50 PMfilesBeingWritten.toList()
-- but probably not the issue herePacane
08/10/2020, 6:50 PMwriteFileToDisk
and it only gets printed to stdout once I cancel the root jobPacane
08/10/2020, 7:02 PMawaitAll
have a timeout mechanism by default?Dominaezzz
08/10/2020, 7:03 PMCasey Brooks
08/10/2020, 7:25 PMcloseMotion
? If it’s a single-threaded dispatcher, then it could be having a deadlock where it spawns an async task, but needs the current task to finish before it can start processing it, but it can’t finish until the new async task has finishedPacane
08/10/2020, 7:26 PMPacane
08/10/2020, 7:26 PMPacane
08/10/2020, 7:27 PMPacane
08/10/2020, 7:28 PM// declared top level
val supervisor = SupervisorJob()
private val motionScope = CoroutineScope(supervisor + EmptyCoroutineContext)
private var motionTimer: Job? = null
private suspend fun updateMotionTimer() {
motionTimer?.cancel()
motionTimer = motionScope.launch {
val motionDirectory = // Getting the path
delay(RECORDING_DURATION_AFTER_MOTION_IN_SECONDS.seconds)
if (isActive) {
closeMotion(motionDirectory)
}
}
}
octylFractal
08/10/2020, 7:31 PMfilesBeingWritten
and reading it from a different thread, it might have concurrency issues -- I'm not clear if your code is written in a way that avoids thatPacane
08/10/2020, 7:32 PMoctylFractal
08/10/2020, 7:33 PMoctylFractal
08/10/2020, 7:33 PMPacane
08/10/2020, 7:34 PMPacane
08/10/2020, 7:34 PMPacane
08/10/2020, 7:34 PMSam Garfinkel
08/11/2020, 8:55 PMSam Garfinkel
08/11/2020, 8:56 PMasad.awadia
08/17/2020, 5:46 PM