Anders Sveen
04/24/2020, 6:02 PMRobert Jaros
04/24/2020, 6:46 PMmp
04/24/2020, 9:32 PM<http://Dispatchers.IO|Dispatchers.IO>
around some blocking i/o or similarAnders Sveen
04/26/2020, 4:50 PMsyncLib.syncMethod(parm1) { syncLibContext ->
call.respond(syncLibContext.value)
}
This of course gives the 'Suspend function 'respond' should be called only from a coroutine or another suspend function' . Any pointers is highly appreciated. 🙂 Is it as easy as adding a coroutine context with Dispatchers.IO around the synclib?syncLib.syncMethod(param1) { syncLibContext ->
CoroutineScope(<http://Dispatchers.IO|Dispatchers.IO> + ...).launch {
call.respond(syncLibContext.value)
}
}
Robert Jaros
04/26/2020, 5:11 PMCoroutineScope(<http://Dispatchers.IO|Dispatchers.IO> + ...).launch {
syncLib.syncMethod(param1) { syncLibContext ->
call.respond(syncLibContext.value)
}
}
Anders Sveen
04/26/2020, 5:15 PMRobert Jaros
04/26/2020, 5:17 PMsyncMethod
taking a callback parameter?Anders Sveen
04/26/2020, 5:21 PMjdbi.inTransaction<Unit, RuntimeException> { handle ->
val resultStream = generatorFunc(handle)
call.respondOutputStream(ContentType.parse("application/json")) {
jsonHandler.streamObjectsToJson(resultStream, this, receiptCallback, endCallback)
}
}
Robert Jaros
04/26/2020, 5:31 PMinTransaction
method should return a value.val stream = withContext(<http://Dispatchers.IO|Dispatchers.IO>) {
jdbi.inTransaction<Unit, RuntimeException> { handle ->
val resultStream = generatorFunc(handle)
jsonHandler.streamObjectsToJson(resultStream, this, receiptCallback, endCallback)
resultStream
}
}
call.respondOutputStream(ContentType.parse("application/json")) {
stream
}
Anders Sveen
04/26/2020, 5:42 PMRobert Jaros
04/26/2020, 5:59 PMAnders Sveen
04/26/2020, 6:00 PMjdbi.open().use { handle ->
val resultStream = generatorFunc(handle)
call.respondOutputStream(ContentType.parse("application/json")) {
jacksonFactory.createGenerator(this).use { jacksonGenerator ->
jacksonGenerator.writeStartArray()
resultStream.forEach {
jacksonObjectMapper.writeValue(generator, it)
}
jacksonGenerator.writeEndArray()
}
}
}
Robert Jaros
04/26/2020, 8:09 PMwithContext(<http://Dispatchers.IO|Dispatchers.IO>) { ... }
to make sure it will not block Ktor