bitkid
10/03/2019, 1:51 PMobject Uploader {
@JvmStatic
fun main(args: Array<String>) {
val files = Channel<File>()
GlobalScope.launch {
uploader(files)
}
files.sendBlocking(File("a"))
}
}
reline
10/03/2019, 2:38 PMrunBlocking
e.g.
fun main(args: Array<String>) = runBlocking { ... }
bitkid
10/03/2019, 4:32 PMreline
10/03/2019, 4:57 PMobject Uploader {
@JvmStatic
fun main(args: Array<String>) = runBlocking {
val files = Channel<File>()
GlobalScope.launch {
files.send(File("a"))
}
uploader(files)
}
}
uploader()
to finishuploader()
is an extension of CoroutineScope
, correct?bitkid
10/03/2019, 5:33 PMreline
10/03/2019, 9:59 PMval files = (1..10).map { File("$it") }
val channel = Channel<File>()
launch {
files.forEach {
channel.send(it)
}
channel.close()
}
val time = measureTimeMillis {
coroutineScope {
uploader(channel)
}
}
println(time)
responses.close()
inside uploadWorker()
after your for loopcoroutineScope
like this here is the right way to go about it, but the only alternative I can think of is making uploader()
suspending
suspend fun uploader(files: ReceiveChannel<File>,
nrOfWorkers: Int = 4) = coroutineScope { ... }
Brad Murray
10/04/2019, 1:30 AMbitkid
10/04/2019, 7:26 AM