gpt4 book ai didi

scala - 如何使用 Akka 以一秒的延迟限制 Futures

转载 作者:行者123 更新时间:2023-12-04 00:03:42 34 4
gpt4 key购买 nike

我有一个 URI 列表,我想在请求之间延迟一秒。我该怎么做?

val uris: List[String] = List()
// How to make these URIs resolve 1 second apart?
val responses: List[Future[Response]] = uris.map(httpRequest(_))

最佳答案

您可以从 URI 列表中创建一个 Akka Streams Source,然后 throttle每个 URI 到 Future[Response] 的转换:

def httpRequest(uri: String): Future[Response] = ???

val uris: List[String] = ???

val responses: Future[Seq[Response]] =
Source(uris)
.throttle(1, 1 second)
.mapAsync(parallelism = 1)(httpRequest)
.runWith(Sink.seq[Response])

关于scala - 如何使用 Akka 以一秒的延迟限制 Futures,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53833327/

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