【发布时间】:2018-06-16 06:01:12
【问题描述】:
我一直在玩Dart Isolates,但在使用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));
}
}
在我的示例中,隔离基本上只是每 2 秒打印一次。
从我在docs上读到的内容,我的理解是上面的代码应该:
- 产生一个隔离体
- 立即暂停该隔离
但它不起作用,即使我告诉它暂停,隔离仍在运行,并且每 2 秒打印一次“隔离正在运行”。
我知道您可以通过传入 paused: true 可选参数启动处于暂停状态的隔离:
Isolate.spawn(isolateEntryPoint, receivePort, paused: true)...。但最终,我希望能够在任何时候暂停隔离,而不是立即暂停。
我能找到的关于使用它的唯一文档是官方 dart 文档,所以我可能错误地使用了 isolate.pause() 函数。但无论哪种方式,我们都非常感谢您提供演示此函数正确用法的代码示例。
【问题讨论】:
标签: dart concurrency dart-isolates