如何在Kotlin中异步对多个项目执行网络请求?

时间:2019-11-20 17:03:56

标签: asynchronous kotlin coroutine

我有一个需要从Web服务检索的项目列表。似乎最好的方法是通过Channel,但是我无法让它们同时运行。为简单起见,我将问题简化为带有模拟网络调用的循环的小型应用程序。


import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import java.text.SimpleDateFormat
import java.util.*
import kotlin.random.Random

private val charPool: List<Char> = ('a'..'z') + ('A'..'Z') + ('0'..'9')
private val sdf =SimpleDateFormat("YYYY-MM-dd kk:mm:ss.SSSS")
const val NUMBER_OF_LOOPS = 5

fun main() = runBlocking<Unit> {
    val channel = Channel<String>()
    log("First thing")
    repeat(NUMBER_OF_LOOPS) {
        launch {
            channel.send(getValue(it))
        }
    }
    log("Second Thing")
    launch {
        repeat(NUMBER_OF_LOOPS) {
            val value = channel.receive()
            log(value)
        }
    }
    log("All Done.")
}

fun getValue(i: Int): String {
    log(String.format("Starting value for %d", i))
    var rand: String = ""
    repeat(100000) {
        rand = (1..256)
            .map { Random.nextInt(0, charPool.size) }
            .map(charPool::get)
            .joinToString("")
    }
    log(String.format("Done processing for %d", i))
    return rand
}

fun log(value: String){
    println(String.format("%s %s", sdf.format(Date()), value))
}

运行此命令时,得到以下输出。

2019-11-20 10:57:00.0473 First thing
2019-11-20 10:57:00.0484 Second Thing
2019-11-20 10:57:00.0485 All Done.
2019-11-20 10:57:00.0495 Starting value for 0
2019-11-20 10:57:02.0489 Done processing for 0
2019-11-20 10:57:02.0495 Starting value for 1
2019-11-20 10:57:03.0898 Done processing for 1
2019-11-20 10:57:03.0898 Starting value for 2
2019-11-20 10:57:05.0270 Done processing for 2
2019-11-20 10:57:05.0270 Starting value for 3
2019-11-20 10:57:06.0607 Done processing for 3
2019-11-20 10:57:06.0607 Starting value for 4
2019-11-20 10:57:07.0930 Done processing for 4
2019-11-20 10:57:07.0930 YxjtbzPR29etkoCg6yR64XwepRPDHGaS8MAp4M16FLyEIaVU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KnU71Y5U22n75i9AQloZpFAbGdEXHduYKWoAHvi8mmPCmK7X1y8wUjqHWYEC4GFgGbrzM4lIPAKp1WagqnL3gndpGMPwY1WpiXtpjqtgGCNb26TEsF178uXgMHk5i8V46bBBNGPFPTtACCn5Ga5myv7GAcwWpGcl4yHkBpxSVuqA1bhssJ4KhIvwexPH159ePeeaixeIBZQTfwDl86

我希望所有“起始值...”日志都出现在任何“完成处理”日志之前,但是您可以看到它们都在同步运行。

有更好的方法吗?

1 个答案:

答案 0 :(得分:1)

您的代码的主要问题是启动子协程的协程范围由runBlocking提供,默认情况下实际上是runs everything in the current thread,在这种情况下,您的{{1} }线程。 main创建的子协程继承了该上下文。如果将launch的输出添加到Thread.currentThread().name函数中,则可以轻松地看到这一点。

将调度程序指定为log的参数,例如:

runBlocking

话虽这么说,我觉得您正在使渠道复杂化。您可以只使用runBlocking(Dispatchers.Default) { ... } 来使这些请求并发。

例如,如果您将async方法更改为以下内容:

main

它应该输出类似以下的内容,我认为这正是您想要的。注意,我将线程名称添加到您的// Note the explicit specification of the dispatcher here... fun main() = runBlocking<Unit>(Dispatchers.Default) { log("First thing") val results = (1..NUMBER_OF_LOOPS).map { async { getValue(it) } }.awaitAll() log("Results: $results") log("All Done.") } 方法中以获取更多信息:

log

如果您不打电话给DefaultDispatcher-worker-4 2019-11-20 14:51:23.0884 Starting value for 3 DefaultDispatcher-worker-1 2019-11-20 14:51:23.0884 Starting value for 1 DefaultDispatcher-worker-5 2019-11-20 14:51:23.0884 Starting value for 4 DefaultDispatcher-worker-6 2019-11-20 14:51:23.0884 Starting value for 5 DefaultDispatcher-worker-3 2019-11-20 14:51:23.0884 Starting value for 2 DefaultDispatcher-worker-4 2019-11-20 14:51:25.0547 Done processing for 3 DefaultDispatcher-worker-6 2019-11-20 14:51:25.0599 Done processing for 5 DefaultDispatcher-worker-1 2019-11-20 14:51:25.0601 Done processing for 1 DefaultDispatcher-worker-5 2019-11-20 14:51:25.0775 Done processing for 4 DefaultDispatcher-worker-3 2019-11-20 14:51:25.0779 Done processing for 2 DefaultDispatcher-worker-3 2019-11-20 14:51:25.0780 Results: [U5VZ..., J4u0..., HWqg..., 1VrO..., ecS1...] DefaultDispatcher-worker-3 2019-11-20 14:51:25.0780 All Done. ,则会得到awaitAll()的列表,您可以编写该列表,例如

Deferred<T>

我建议阅读Composing Suspending Functions,尤其是有关结构化并发的部分。使用异步功能正确处理故障可能会很棘手,在将此类代码投入生产之前,需要额外注意。