简体   繁体   English

Dart有调度程序吗?

[英]Does Dart have a scheduler?

I am looking at dart from server side point of view. 我从服务器端的角度来看dart。

Is there a scheduler that can execute isolates at a specific time or X times an hour? 是否有可以在特定时间或每小时X次执行隔离的调度程序? I am thinking on the lines of Quartz in the Java world. 我正在思考Java世界中的Quartz。

Dart has a few options for delayed and repeating tasks, but I'm not aware of a port of Quartz to Dart (yet... :) Dart有一些延迟和重复任务的选项,但我不知道Quartz to Dart的端口(还是...... :)

Here are the basics: 以下是基础知识:

  • Timer - simply run a function after some delay Timer - 在一段延迟后简单地运行一个函数
  • Future - more robust, composable, functions that return values "in the future" Future - 更强大,可组合的功能,在未来“回归”价值
  • Stream - robust, composable streams of events. Stream - 强大,可组合的事件流。 Can be periodic. 可以是定期的。

If you have a repeating task, I would recommend using Stream over Timer. 如果您有重复任务,我建议使用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). Timer没有内置的错误处理,因此未捕获的异常可以降低整个程序(Dart没有全局错误处理程序)。

Here's how you use a Stream to produce periodic results: 以下是使用Stream生成定期结果的方法:

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. 或者,您可以在程序启动时生成隔离,并且隔离本身可以运行自己的计时器或定期流。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM