https://kotlinlang.org logo
Title
d

dave08

12/06/2017, 3:37 AM
Say
suspend fun getX()
needs to be able to be run either in main thread context using
CommonPool
, or in an existing
coroutineContext
or in an already existing worker thread in a blocking fashion, depending on from where it's called, what's the recommended way to do each, and when would I have to
yield
?
e

elizarov

12/06/2017, 8:11 AM
The recommend practice is to write
suspend fun getX()
as you would normally do and delegate decision on what context to run it in to its caller.
d

dave08

12/06/2017, 8:18 AM
The
getX()
in my case is the
channel.send(parsedApiActions)
, that is supposed to dispatch actions to the channel that will fan out to handlers and fan in again for results, the results need to be recieved on the current thread but the processing is done in a thread pool...
e

elizarov

12/06/2017, 8:21 AM
The context shall be specified around the context-sensitive code. If your handlers need to be run a specific thread, they have to be launched in the context of the specific thread. The code that invoked
send
should not be even remotely concerned about that.