Kotlin Vertx中的协同程序从未执行?

Kotlin Vertx中的协同程序从未执行?,kotlin,vert.x,kotlin-coroutines,Kotlin,Vert.x,Kotlin Coroutines,在Vert.x中,假设我有如下函数: fun caller() { runBlocking { val job = GlobalScope.launch(vertx.dispatcher()) { val r = suspendPart() println(r) // never execute } println(1) // printed job.join()

在Vert.x中,假设我有如下函数:

fun caller() {
    runBlocking {
        val job = GlobalScope.launch(vertx.dispatcher()) {
            val r = suspendPart()
            println(r) // never execute
        }
        println(1) // printed
        job.join()
        println(2) // never execute
    }
}

suspend fun asyncPart(): Future<Int> {
    val promise: Promise<Int> = Promise.promise()
    delay(500)
    promise.complete(0)
    return promise.future()
}

suspend fun suspendPart(): Int {
    return asyncPart().await()
}
fun caller(){
运行阻塞{
val job=GlobalScope.launch(vertx.dispatcher()){
val r=suspendPart()
println(r)//从不执行
}
println(1)//已打印
job.join()
println(2)//从不执行
}
}
suspend fun asyncPart():未来{
val promise:promise=promise.promise()
延迟(500)
承诺。完成(0)
回报承诺未来
}
suspend fun suspendPart():Int{
返回asyncPart().等待()
}
r(为0)和2永远不会打印,只打印1。我该怎么修?
我的意图是等待
asyncPart
完成(实际上我有一个
AsyncResult

大概你的
caller()
方法是由vert.x调用的,这意味着你正在打破vert.x的一个关键规则:

别挡我! Vert.x主要基于非常快速的单线程工作,这意味着当您在
调用者
中阻塞线程时,它无法执行与
启动
计划的协同程序,从而导致死锁

解决这个问题的正确方法是通过vert.x为kotlin协程提供的集成来删除阻塞代码


或者,为
launch
使用不同的调度程序也会起作用,因为另一个线程会解除对vert.x调度程序的阻塞。但这并不能解决vert.x dispatcher中阻塞调用的主要问题。

Kiskae,这些是我的应用程序初始代码的一部分,稍微阻塞一点对我来说是可以接受的。在我的情况下,我需要等待
AsyncResult
await()
函数完美地工作。但是,使用
await()
函数需要我使函数
挂起
,并且要调用挂起函数,我需要使用与
runBlocking
的协程。您提到使用不同的调度器可以工作,您能举个例子吗?谢谢。我根据您的建议找到了一个简单的解决方案,只需创建一个新顶点并使用它创建一个调度程序:
Vertx.Vertx().dispatcher()
我根据您的建议找到了一个简单的解决方案,只需创建一个新顶点并使用它创建一个调度程序:
Vertx.Vertx().dispatcher()