Dart有调度程序吗? [英] Does Dart have a scheduler?

查看:127
本文介绍了Dart有调度程序吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从服务器端看dart。

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

是否有可以在特定时间或每小时执行一次隔离的调度程序?我正在考虑在Java世界中的Quartz的线。

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.

推荐答案

Dart有延迟和重复任务的几个选项,但我不知道Quartz的端口Dart但...:)

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

以下是基本信息:


  • Timer - 延迟后只需运行一个函数

  • ,在未来返回值的函数

  • - 强大的,可组合的事件流。可以是周期性的。

  • 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.

如果您有重复任务,我建议使用Stream over Timer。定时器没有内置的错误处理,所以未捕获的异常可能会导致你的整个程序(Dart没有全局错误处理程序)。

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).

这里是如何使用 Stream 以产生定期结果:

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.

这篇关于Dart有调度程序吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆