gotoOla
03/12/2020, 1:32 PMclass MyQueueProcessor: CoroutineScope by CouroutineScope(<http://Dispatchers.IO|Dispatchers.IO>) {
suspend fun start() {
pollMessages()
processors.addAll(
(1..5).map {
launchMessageProcessor()
}
)
}
suspend fun pollMessages() = launch {
while(true) {
val messages = //... get messages from a queue
internalChannel.send(messages)
}
}
suspend fun launchMessageProcessor() = launch {
for (messages in internalChannel) {
// process each 'messages'
}
}
}
but for both pollMessages and launchMessageProcessor I get "Ambiguous coroutineContext due to CoroutineScope receiver of suspend function". How should I interpret this?
Is it because suspend fun start() is public and you inherit the coroutineContext from the caller? Since I extend CoroutineScope, the launch and async stuff I use should run on that scope with that dispatcher (IO) right?dekans
03/12/2020, 2:06 PMsuspend fun launchMessageProcessor() = launch {}
doesn't make sense.
launch
is not a suspend functiondekans
03/12/2020, 2:06 PMsuspend
keywordEvan R.
03/12/2020, 2:06 PMlaunch
should occur in the caller’s coroutine scope or the coroutine scope of the classdekans
03/12/2020, 2:07 PMlaunch
and launchMessageProcessor
becomes a real suspend functiondekans
03/12/2020, 2:07 PMlaunch {..}
dekans
03/12/2020, 2:07 PMpollMessages()
gotoOla
03/12/2020, 7:17 PM= this.launch {...}
should clarify that ambigouity if I remove the suspend from the function then, right?Evan R.
03/12/2020, 7:17 PMgotoOla
03/12/2020, 10:15 PM