加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Scala嵌套期货

发布时间:2020-12-16 18:36:06 所属栏目:安全 来源:网络整理
导读:我有几个未来. campaignFuture返回一个List [BigInt],我希望能够为第一个返回的列表中的每个值调用第二个将来的profileFuture.第二个未来只能在第一个未来完成时调用.我如何在 Scala中实现这一目标? campaignFuture(1923).flatMap?? (May be?)def campaignF
我有几个未来. campaignFuture返回一个List [BigInt],我希望能够为第一个返回的列表中的每个值调用第二个将来的profileFuture.第二个未来只能在第一个未来完成时调用.我如何在 Scala中实现这一目标?

campaignFuture(1923).flatMap?? (May be?)

def campaignFuture(advertiserId: Int): Future[List[BigInt]] = Future {
  val campaignHttpResponse = getCampaigns(advertiserId.intValue())
  parseProfileIds(campaignHttpResponse.entity.asString)
}

def profileFuture(profileId: Int): Future[List[String]] = Future {
  val profileHttpResponse = getProfiles(profileId.intValue())
  parseSegmentNames(profileHttpResponse.entity.asString)
}

解决方法

理解是不适用的,因为我们混合了List和Future.
所以,你的朋友是map和flatMap:

对未来结果作出反应

import scala.concurrent.{Future,Promise,Await}
  import scala.concurrent.duration.Duration
  import scala.concurrent.ExecutionContext.Implicits.global

  def campaignFuture(advertiserId: Int): Future[List[BigInt]] = Future {
    List(1,2,3)
  }
  def profileFuture(profileId: Int): Future[List[String]] = {
    // delayed Future
    val p = Promise[List[String]]
    Future {
      val delay: Int = (math.random * 5).toInt
      Thread.sleep(delay * 1000)
      p.success(List(s"profile-for:$profileId",s"delayed:$delay sec"))
    }
    p.future
  }



  // Future[List[Future[List[String]]]
  val listOfProfileFuturesFuture = campaignFuture(1).map { campaign =>
    campaign.map(id => profileFuture(id.toInt))
  }

  // React on Futures which are done
  listOfProfileFuturesFuture foreach { campaignFutureRes =>
    campaignFutureRes.foreach { profileFutureRes =>
      profileFutureRes.foreach(profileListEntry => println(s"${new Date} done: $profileListEntry"))
    }
  }


  // !!ONLY FOR TESTING PURPOSE - THIS CODE BLOCKS AND EXITS THE VM WHEN THE FUTURES ARE DONE!!
  println(s"${new Date} waiting for futures")
  listOfProfileFuturesFuture.foreach{listOfFut =>
    Await.ready(Future.sequence(listOfFut),Duration.Inf)
    println(s"${new Date} all futures done")
    System.exit(0)
  }
  scala.io.StdIn.readLine()

一次性获得所有期货的结果

import scala.concurrent.{Future,3)
  }
  def profileFuture(profileId: Int): Future[List[String]] = Future {
    List(s"profile-for:$profileId")
  }


  // type: Future[List[Future[List[String]]]]
  val listOfProfileFutures = campaignFuture(1).map { campaign =>
    campaign.map(id => profileFuture(id.toInt))
  }

  // type: Future[List[List[String]]]
  val listOfProfileFuture = listOfProfileFutures.flatMap(s => Future.sequence(s))


  // print the result
  //listOfProfileFuture.foreach(println)
  //scala.io.StdIn.readLine()

  // wait for the result (THIS BLOCKS INFINITY!)
  Await.result(listOfProfileFuture,Duration.Inf)

>我们使用Future.sequence将List [Future [T]]转换为Future [List [T]].
> flatMap从未来[未来[T]]获得未来[T]
>如果您需要等待结果(BLOCKING!),请使用Await等待结果

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读