相关疑难解决方法(0)

Dart孤立的暂停功能未按预期工作

我一直在玩Dart孤立,并且在使用该isolate.pause();功能时遇到问题。

import 'dart:io';
import 'dart:isolate';

main(){
  ReceivePort receivePort = new ReceivePort();
  Isolate.spawn(isolateEntryPoint, receivePort.sendPort).then((isolate){
    isolate.pause(isolate.pauseCapability);
  });
}

void isolateEntryPoint(SendPort sendPort){
  while(true){
    print("isolate is running");
    sleep(new Duration(seconds: 2));
  }
}
Run Code Online (Sandbox Code Playgroud)

在我的示例中,隔离区基本上只是每2秒打印出一些内容。

从我阅读的文档中,我的理解是上面的代码应该:

  1. 产生隔离
  2. 立即暂停隔离

但是它不起作用,隔离程序仍在运行,即使我告诉它暂停,它也会每2秒打印一次“隔离程序正在运行”。

我知道您可以通过传递可选参数 来在暂停状态下启动隔离。但最终,我希望能够在任何时候都暂停隔离,而不仅仅是立即行动。paused: trueIsolate.spawn(isolateEntryPoint, receivePort, paused: true)...

我可以找到的关于此功能的唯一文档在官方的dart文档中,因此我可能使用了该isolate.pause()函数不正确。但是,无论哪种方式,演示此功能正确用法的代码示例都将不胜感激。

concurrency dart dart-isolates

3
推荐指数
1
解决办法
146
查看次数

标签 统计

concurrency ×1

dart ×1

dart-isolates ×1