1

我将Monix用于异步任务工作流。

我们如何杀死跑步者Task

Task{ println("sleep")
      Thread.sleep(200)
      println("effect") }
.doOnCancel(Task(println("canceled")))
.timeout(100.milli) // timeout will do cancel
.runOnComplete(println)

@> Failure(java.util.concurrent.TimeoutException: Task timed-out after 100 milliseconds of inactivity) sleep canceled effect <--- what !? , task is running. Isn't it canceled !?

我认为我目前的解决方案很丑(标志检查阻碍了代码重用):

var flag=true
Task{ 
      println("sleep")
      Thread.sleep(200)
      if (flag)
        println("effect") 
}
.doOnCancel(Task{ flag=false; println("canceled") })
.timeout(100.milli) // timeout will do cancel

如果不可能,我们如何在 not-yet-ran 时杀死计划的Task

我失败的尝试是:

Task{ println("sleep"); Thread.sleep(200) }
.map{ _ => println("effect") }
.doOnCancel(Task(println("canceled")))
.timeout(100.milli) // timeout will do cancel
.runOnComplete(println)

可悲的是,它仍然显示取消发生后的效果。我希望可以取消已计划且尚未运行的任务(这.map(...)是另一个Task,对吗?)

4

2 回答 2

2

If you don't use Thread.sleep (which messes with the internals of Monix), but Task.sleep, things are working just fine.

Task
  .defer {
    println("start")
    Task.sleep(1000.millis)
  }
  .map(_ => println("effect"))
  .timeout(10.millis)
  .doOnCancel(Task(println("canceling")))

Now, the question is what's your actual use case, because I'm sure you used Thread.sleep just for illustration purposes.

于 2018-05-17T13:35:48.083 回答
0

如果是任务链,我找到了一种解决方案:

Task{println("start");Thread.sleep(1000)}
 .asyncBoundary
 .map{_=> println("effect")}
 .doOnCancel(Task(println("canceling")))
 .timeout(10.milli)
 .executeWithOptions(_.enableAutoCancelableRunLoops)
 .runOnComplete(println)

参考:https ://github.com/monix/monix/issues/226

但我希望有简单的方法来中断任务,而不是使用闭包或拆分和链接任务。

于 2018-05-14T10:32:17.283 回答