Created
March 24, 2019 16:32
-
-
Save liberaid2/2e22b6c71bc7f6ac321a5a8fb55a2618 to your computer and use it in GitHub Desktop.
Coroutines Pool for task handling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import kotlinx.coroutines.CompletableDeferred | |
import kotlinx.coroutines.CoroutineScope | |
import kotlinx.coroutines.GlobalScope | |
import kotlinx.coroutines.channels.* | |
import kotlinx.coroutines.launch | |
import kotlinx.coroutines.selects.select | |
class TaskPool<T, R> (private val taskChannel: ReceiveChannel<T>, private val taskHandler: suspend CoroutineScope.(T) -> R, poolCapacity: Int = 5) { | |
private val resultChannel = Channel<R>(poolCapacity) | |
val results: ReceiveChannel<R> | |
get() = resultChannel | |
private var tasksRun = CustomAtomic(false) | |
private val workers = List(poolCapacity) { | |
val ack = CompletableDeferred<Boolean>() | |
GlobalScope.actor<T> { | |
for (task in this) { | |
resultChannel.send(taskHandler(task)) | |
} | |
ack.complete(true) | |
} to ack | |
} | |
fun runTasks() = GlobalScope.launch { | |
if(tasksRun.getValue()){ | |
throw RuntimeException("Tasks are already running") | |
} | |
tasksRun.setValue(true) | |
for (task in taskChannel) { | |
select<Unit> { | |
workers.forEach { (worker, _) -> | |
worker.onSend(task) {} | |
} | |
} | |
} | |
workers.forEach { (worker, ack) -> | |
worker.close() | |
ack.await() | |
} | |
resultChannel.close() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage example