1
使用Akka的調度程序時,我遇到了一個奇怪的行爲。我的代碼看起來大致是這樣的:什麼會導致Akka的調度程序在預定時間之前執行預定任務?
val s = ActorSystem("scheduler")
import scala.concurrent.ExecutionContext.Implicits.global
def doSomething(): Future[Unit] = {
val now = new GregorianCalendar(TimeZone.getTimeZone("UTC"))
println(s"${now.get(Calendar.MINUTE)}:${now.get(Calendar.SECOND)}:${now.get(Calendar.MILLISECOND)}")
// Do many things that include an http request using "dispatch" and manipulation of the response and saving it in a file.
}
val futures: Seq[Future[Unit]] = for (i <- 1 to 500) yield {
println(s"$i : ${i*600}")
// AlphaVantage recommends 100 API calls per minute
akka.pattern.after(i * 600 milliseconds, s.scheduler) { doSomething() }
}
Future.sequence(futures).onComplete(_ => s.terminate())
當我執行我的代碼,doSomething
最初反覆連續調用之間600毫秒叫,符合市場預期。但是,過了一段時間,所有剩餘的預定呼叫突然同時執行。
我懷疑我的doSomething
裏面的東西可能會干擾調度,但我不知道是什麼。我的doSomething
只是使用調度來處理http請求並操縱結果,並且不以任何方式直接與akka或調度程序進行交互。所以,我的問題是:
什麼會導致調度程序的調度失敗,並突然觸發立即執行所有剩餘的調度任務?
(PS:我試圖簡化我doSomething
張貼在這裏最小非工作例子,但我的簡化導致工作例子。)