Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Dart 无法暂停省道隔离_Dart_Dart Isolates - Fatal编程技术网

Dart 无法暂停省道隔离

Dart 无法暂停省道隔离,dart,dart-isolates,Dart,Dart Isolates,我希望在Dart中创建一个可以通过编程暂停和恢复的隔离。这是我使用的代码 导入'dart:io'; 导入“dart:隔离”; void main()异步{ 打印(“开始隔离”); 隔离; ReceivePort ReceivePort=ReceivePort(); 隔离=等待隔离.spawn(run,receivePort.sendPort); 打印(“暂停”); 能力上限=隔离.暂停(隔离.暂停能力); 睡眠(持续时间:5秒); 打印(“恢复”); 隔离。恢复(cap); } 无效运行(发送端

我希望在Dart中创建一个可以通过编程暂停和恢复的隔离。这是我使用的代码

导入'dart:io';
导入“dart:隔离”;
void main()异步{
打印(“开始隔离”);
隔离;
ReceivePort ReceivePort=ReceivePort();
隔离=等待隔离.spawn(run,receivePort.sendPort);
打印(“暂停”);
能力上限=隔离.暂停(隔离.暂停能力);
睡眠(持续时间:5秒);
打印(“恢复”);
隔离。恢复(cap);
}
无效运行(发送端口发送端口){
睡眠(持续时间:2秒);
打印(“醒来,1”);
睡眠(持续时间:2秒);
打印(“醒来,2”);
睡眠(持续时间:2秒);
打印(“醒来,3”);
睡眠(持续时间:2秒);
打印(“醒来,4”);
睡眠(持续时间:2秒);
打印(“醒来,5”);
}
我得到的O/p是这样的

Starting isolate
pausing
Woke up, 1
Woke up, 2
Resuming
Woke up, 3
Woke up, 4
Woke up, 5
但我想实现

Starting isolate
pausing
<--- 5 second delay --->
Resuming
Woke up, 1
Woke up, 2
Woke up, 3
Woke up, 4
Woke up, 5
启动隔离
暂停
恢复
醒来,1
醒来,2
醒来,3
醒来,4
醒来,5点

但即使在调用pause()之后,隔离仍会继续运行。我发现,它说,这是因为在他们的情况下,无限循环,但我没有使用任何循环。那么,我做错了什么呢?

您的问题是,您在文档中遗漏了关于隔离的一个细节:

当隔离收到pause命令时,它停止处理事件循环队列中的事件。它仍然可以向队列中添加新事件以响应计时器或接收端口消息等。恢复隔离后,它将开始处理已排队的事件

因此,
pause
不会停止方法的执行,只会确保在执行当前正在运行的方法之后,它将停止从事件队列中提供更多的工作

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

Future<void> main() async {
  print("Starting isolate");
  Isolate isolate;
  ReceivePort receivePort = ReceivePort();
  isolate = await Isolate.spawn(run, receivePort.sendPort);
  print("pausing");
  Capability cap = isolate.pause(isolate.pauseCapability);
  sleep(const Duration(seconds: 5));
  print("Resuming");
  isolate.resume(cap);
}

Future<void> run(SendPort sendPort) async {
  await Future<void>.delayed(const Duration(seconds: 2));
  print("Woke up, 1");
  await Future<void>.delayed(const Duration(seconds: 2));
  print("Woke up, 2");
  await Future<void>.delayed(const Duration(seconds: 2));
  print("Woke up, 3");
  await Future<void>.delayed(const Duration(seconds: 2));
  print("Woke up, 4");
  await Future<void>.delayed(const Duration(seconds: 2));
  print("Woke up, 5");
}
While的实现更像是“立即停止所有执行N次”,这更符合您的期望:

请小心使用,因为在睡眠调用中被阻止时,隔离中不能处理任何异步操作

要使示例发挥作用,您需要将
sleep
调用替换为您正在等待的
Future
实例,因为这将在事件队列中添加事件

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

Future<void> main() async {
  print("Starting isolate");
  Isolate isolate;
  ReceivePort receivePort = ReceivePort();
  isolate = await Isolate.spawn(run, receivePort.sendPort);
  print("pausing");
  Capability cap = isolate.pause(isolate.pauseCapability);
  sleep(const Duration(seconds: 5));
  print("Resuming");
  isolate.resume(cap);
}

Future<void> run(SendPort sendPort) async {
  await Future<void>.delayed(const Duration(seconds: 2));
  print("Woke up, 1");
  await Future<void>.delayed(const Duration(seconds: 2));
  print("Woke up, 2");
  await Future<void>.delayed(const Duration(seconds: 2));
  print("Woke up, 3");
  await Future<void>.delayed(const Duration(seconds: 2));
  print("Woke up, 4");
  await Future<void>.delayed(const Duration(seconds: 2));
  print("Woke up, 5");
}

您的输出是什么?我已经用所需的和当前的O/PSo编辑了这个问题。这是否意味着如果
run()
方法中没有我们正在等待的未来,那么整个
run()
方法将被视为单个队列事件?是的。如果你不等待任何未来(或流),程序将只是一个大事件,不能暂停。因此,如果你需要做一些繁重的计算,并希望能够暂停它,你需要将它分成多个事件,或者让它有时等待未来。您可以生成一个空的未来并毫不延迟地等待它,比如:
await Future.value()。它会产生一点开销,所以要做一个计数器,这样它只会对每N次迭代执行一次。