Does Dart have a scheduler?

≡放荡痞女 提交于 2019-12-19 02:40:50

问题


I am looking at dart from server side point of view.

Is there a scheduler that can execute isolates at a specific time or X times an hour? I am thinking on the lines of Quartz in the Java world.


回答1:


Dart has a few options for delayed and repeating tasks, but I'm not aware of a port of Quartz to Dart (yet... :)

Here are the basics:

  • Timer - simply run a function after some delay
  • Future - more robust, composable, functions that return values "in the future"
  • Stream - robust, composable streams of events. Can be periodic.

If you have a repeating task, I would recommend using Stream over Timer. Timer does not have error handling builtin, so uncaught exceptions can bring down your whole program (Dart does not have a global error handler).

Here's how you use a Stream to produce periodic results:

import 'dart:async';

main() {
  var stream = new Stream.periodic(const Duration(hours: 1), (count) {
    // do something every hour
    // return the result of that something
  });

  stream.listen((result) {
    // listen for the result of the hourly task
  });
}

You specifically ask about isolates. You could spawn an isolate at program start, and send it a message every hour. Or, you can spawn the isolate at program start, and the isolate itself can run its own timer or periodic stream.



来源:https://stackoverflow.com/questions/15848214/does-dart-have-a-scheduler

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!