【发布时间】:2020-08-05 07:31:03
【问题描述】:
我想一次发送 1000 条记录,每批 500 条。一旦发送,我希望尾递归继续。 numberOfEventsPerSecond=1000 putRecordLimit=500 在 furure 调用完成之前调用 sendInBatches。有什么办法先完成futureCall调用然后只调用sendInBatches。
@scala.annotation.tailrec
final def sendBatches(
buffer: Seq[File],
numberOfEventsPerSecond: Int
): Seq[PutRecordsRequestEntry] =
{
val (listToSend, remaining) = buffer.splitAt(numberOfEventsPerSecond)
val listRes = listToSend
.grouped(putRecordLimit)
.toList
.filter(_.nonEmpty)
listRes.map { list =>
futureCall(list.filter(_ != null)) map { putDataResult =>
println("Sent")
)
}
}
sendInBatches(fileOption, remaining, numberOfEventsPerSecond)
}
【问题讨论】:
-
Lists 和 Futures 在您需要批处理和并行性之类的东西时不是一个好主意。我建议你看看流媒体库,比如
Akka Streams、fs2、monix或zio zstreams。
标签: scala scala-collections tail-recursion