gpt4 book ai didi

scala - 玩转 2.0 Scala 和异步回调

转载 作者:行者123 更新时间:2023-12-04 17:53:28 24 4
gpt4 key购买 nike

我想对我的数据库进行两次调用,这将需要一段时间才能返回结果,而且我不想阻塞当前线程。我使用 Akka Futures 来包装数据库调用。

我不想等待(阻塞)两个调用返回,而是指定要调用的回调函数,然后可以呈现响应。我怎么做?这是我的 Controller 代码:

def showPie = IsAuthenticated(Roles.validator) { user => implicit request =>
val eventUid = request.session.get(EventUid).get

val printed = Akka.future(TicketRepository.getCountForState(eventUid, "Printed"))
val validated = Akka.future(TicketRepository.getCountForState(eventUid, "Validated"))

//this would be evil, because it would block: Ok(views.html.pie(printed.await(1000).get, validated.await(1000).get))

//create a promise for all the promised results
val promise = Promise.sequence(List(printed, validated))

//this doesnt work, but how can I make it work WITHOUT blocking this thread?
promise.callWhenResultIsReady(Ok(view.html.pie(promise.get))
}

最佳答案

你很接近。您可以简单地调用 map promise 来处理它。在 Async block 内,它保持非阻塞。 Relevant documentation (参见“AsyncResult”)。

def showPie = IsAuthenticated(Roles.validator) { user => implicit request =>
val eventUid = request.session.get(EventUid).get

val printed = Akka.future(TicketRepository.getCountForState(eventUid, "Printed"))
val validated = Akka.future(TicketRepository.getCountForState(eventUid, "Validated"))

//create a promise for all the promised results
val promise = Promise.sequence(List(printed, validated))
Async {
promise map { res =>
Ok("Got it!" + res)
}
}
}

编辑:从您下面的评论中,让我们仔细看看 Async block 。 Async 接受一个 Promise,并返回一个 AsyncResult,它是 Result 的子类型( >行动需要)。

    Async {
// We take the promise, and add something akin to a callback
// function with `map`. This new function is called when `promise`
// is complete.
val result = promise map { res => // this is the redeemed promise
Ok("Got it!" + res)
}
result // this is the new promise
}

由于 map 函数在 promise 完成时被调用,因此它保持非阻塞。整个 block 快速返回一个 AsyncResult,开始玩吧!以类似的方式管理它,在它完成时返回给客户端(同时释放 Play! 去做其他事情)。

关于scala - 玩转 2.0 Scala 和异步回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12585232/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com