jonathan
12/04/2018, 1:48 AMclass TSCompiler {
private val dispatcher = Executors.newFixedThreadPool(24).asCoroutineDispatcher()
public suspend fun compile(program: Any) = coroutineScope {
launch(dispatcher) {
// do something that takes awhile!
}
}
}
Would it be better in this case to just use the Executor directly, rather than using it as a coroutine Dispatcher?gildor
12/04/2018, 1:53 AMprogram
can be suspendable itself and support cancellation, but it requires another approach for such executorgildor
12/04/2018, 1:54 AMwithContext(dispatcher)
gildor
12/04/2018, 1:54 AMprogram
is blocking or Default
if it’s CPU consumingjonathan
12/04/2018, 2:24 AMgildor
12/04/2018, 2:25 AMcompile
function, not just Jobgildor
12/04/2018, 2:26 AMjonathan
12/04/2018, 2:28 AMgildor
12/04/2018, 2:30 AMpublic suspend fun compile(program: Any): CompiationResult {
return withContext(YOUR_DISPATCHER) {
doBlockingCompilation(program)
}
}
gildor
12/04/2018, 2:33 AMpost("/compile") {
val program = call.request.body()
val compiled = compile(program) // Non blocking compilation
call.respond(compiled.logs) // we can use result when it's ready
}