第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

您將如何使用 Scala 的異步超時(shí)多個(gè)異步請求?

您將如何使用 Scala 的異步超時(shí)多個(gè)異步請求?

Go
藍(lán)山帝景 2021-07-31 13:51:20
我不知道 Scala,但我很好奇它的異步功能(類似于 C# 的)。你如何將這個(gè) go 代碼翻譯成 Scala 異步?http://talks.golang.org/2012/concurrency.slide#47c := make(chan Result)go func() { c <- Web(query) } ()go func() { c <- Image(query) } ()go func() { c <- Video(query) } ()timeout := time.After(80 * time.Millisecond)for i := 0; i < 3; i++ {    select {    case result := <-c:        results = append(results, result)    case <-timeout:        fmt.Println("timed out")        return    }}return
查看完整描述

1 回答

?
慕村225694

TA貢獻(xiàn)1880條經(jīng)驗(yàn) 獲得超4個(gè)贊

這是如何完成的草圖(未經(jīng)測試;我不聲稱這是最好的解決方案):


// I assume that the Web/Image/Video functions return instances of Future[Result]

val f1 = Web(query)

val f2 = Image(query)

val f3 = Video(query)

val t = timeout(80.milliseconds)


// using Scala's Future API

val results: Future[Seq[Result]] = for {

  r1 <- or(f1)(t)

  r2 <- or(f2)(t)

  r3 <- or(f3)(t)

} yield (r1.toSeq ++ r2.toSeq ++ r3.toSeq)


// OR using async

val results: Future[Seq[Result]] = async {

  val r1 = or(f1)(t)

  val r2 = or(f2)(t)

  val r3 = or(f3)(t)

  await(r1).toSeq ++ await(r2).toSeq ++ await(r3).toSeq

}


// or and timeout are utility/library functions defined below


def or[T](f1: Future[T])(f2: Future[Option[Nothing]]): Future[Option[T]] =

  Future.firstCompletedOf(f1 map Some.apply, f2)


// create a future that will complete successfully with None

// after the given duration passes

def timeout(d: Duration): Future[Option[Nothing]] = {

  val p = Promise[Option[Nothing]]

  Scheduler.after(d) { p success None }

  p.future

}


查看完整回答
反對 回復(fù) 2021-08-02
  • 1 回答
  • 0 關(guān)注
  • 198 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)