如何在 Play Framework 2 控制器操作中等待?

How to Await in Play Framework 2 controller action?

我尝试编写操作来生成图片的缩略图,并在尚未生成时向用户显示。 问题是:生成部分异步运行并显示部分代码在生成部分完成之前运行。 我是 Scala 和 Play Framework 的新手,不能让表演部分等待。我试过这样:

def thumbs(profiletype: String, id: Int, filename: String, extention: String) = Action {
val content = File.getBinaryContent(pathToDir + filename + "." + extention)
content match {
  case Some(c) => Ok(c).as(File.getContentType(extention))
  case _ => val picture = Picture.getById(id)
      picture match {
        case None => NotFound("Page not found")
        case Some(p) => val rslt = Future (p.generateThumbs(pathToDir, profiletype))
          val r=rslt.map(a=>a)
          Await.ready(r, Duration(8, "second"))
            Logger.debug(pathToDir + filename + "." + extention)
            val content = File.getBinaryContent(pathToDir + filename + "." + extention)
            renderBinary(content, File.getContentType(extention))}
          }
}

像这样运气不好

def thumbs(profiletype: String, id: Int, filename: String, extention: String) = Action {
val content = File.getBinaryContent(pathToDir + filename + "." + extention)
content match {
  case Some(c) => Ok(c).as(File.getContentType(extention))
  case _ => val picture = Picture.getById(id)
      picture match {
        case None => NotFound("Page not found")
        case Some(p) => val rslt = Future (p.generateThumbs(pathToDir, profiletype))
          Await.ready(rslt, Duration(8, "second"))
            Logger.debug(pathToDir + filename + "." + extention)
            val content = File.getBinaryContent(pathToDir + filename + "." + extention)
            renderBinary(content, File.getContentType(extention))}
          }
}

此 Await 不起作用,Logger 在 p.generateThumbs 完成之前记录日志

您可以 return a Future[Result].

而不是异步生成缩略图并在等待生成缩略图时阻塞(使用 Await.readyAwait.result

在你的情况下,这看起来像(未经测试):
我还将你的 Option 模式匹配转换为 mapgetOrElse 函数。

def thumbs(
  profiletype: String, id: Int, filename: String, extention: String
) = Action.async { // return an (explicit) asynchronous result
  val filepath = pathToDir + filename + "." + extention
  File.getBinaryContent(filepath)
    .map ( content => 
       // we found the thumbnail, return its content as a Future[Result]
       Future.successful(Ok(content).as(File.getContentType(extention)))
    )
    .getOrElse {
      // we have not found the thumbnail, check if the Picture exists
      Picture.getById(id)
        .map { picture =>
          // we found the Picture, lets generate the thumbnails
          Future(picture.generateThumbs(pathToDir, profiletype))
            .map { futureResult =>
              // the thumbnails are created
              // return the content of the thumbnail 
              // and since we are in a Future.map this will be a Future[Result]
              Logger.debug(filepath)
              val content = File.getBinaryContent(filepath)
              renderBinary(content, File.getContentType(extention))
            }
        }
        // we could not find the Picture return a NotFound as Future[Result]
        .getOrElse(Future.successful(NotFound("Page not found")))
    }
}

这样我们只有一个阻塞线程来创建缩略图,而不是两个,一个创建缩略图,一个等待第一个线程完成创建缩略图。

Play 可以在创建缩略图时处理其他请求,创建缩略图后 Play 将以缩略图响应。