【发布时间】:2020-02-16 15:01:54
【问题描述】:
我该如何处理异步执行任务的失败? IE。至少打印堆栈跟踪并关闭。下面的代码似乎永远等待输入> 5
val things = Range(1, 40)
implicit val scheduler = monix.execution.Scheduler.global
def t(i:Int) = Task.eval {
Try{
Thread.sleep(1000)
val result = i + 1
if(result > 5){
throw new Exception("asdf")
}
// i.e. write to file, that's why unit is returned
println(result) // Effect
"Result"
}
}
val futures = things.map(e=> t(e))
futures.foreach(_.runToFuture)
编辑
尝试:
futures.foreach(_.runToFuture.onComplete {
case Success(value) =>
println(value)
case Failure(ex) =>
System.err.println(ex)
System.exit(1)
})
不会停止计算。 如何记录堆栈跟踪并取消正在进行的计算并停止?
【问题讨论】: