Ash*_*ngh 3 dart dart-isolates
我希望在 Dart 中创建一个 Isolate,我可以以编程方式暂停和恢复。这是我使用的代码。
import 'dart:io';
import 'dart:isolate';
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(Duration(seconds: 5));
print("Resuming");
isolate.resume(cap);
}
void run(SendPort sendPort) {
sleep(Duration(seconds: 2));
print("Woke up, 1");
sleep(Duration(seconds: 2));
print("Woke up, 2");
sleep(Duration(seconds: 2));
print("Woke up, 3");
sleep(Duration(seconds: 2));
print("Woke up, 4");
sleep(Duration(seconds: 2));
print("Woke up, 5");
}
Run Code Online (Sandbox Code Playgroud)
我得到一个像 O/P
Starting isolate
pausing
Woke up, 1
Woke up, 2
Resuming
Woke up, 3
Woke up, 4
Woke up, 5
Run Code Online (Sandbox Code Playgroud)
但我想达到
Starting isolate
pausing
<--- 5 second delay --->
Resuming
Woke up, 1
Woke up, 2
Woke up, 3
Woke up, 4
Woke up, 5
Run Code Online (Sandbox Code Playgroud)
但即使在调用 pause() 之后,Isolate 仍会继续运行。我找到了这个答案,它说这是因为在他们的情况下是无限循环,但我没有使用任何循环。那么我在这里做错了什么?
您的问题是您错过了pause有关隔离的文档中的详细信息:
当隔离接收到暂停命令时,它停止处理来自事件循环队列的事件。它仍然可以向队列添加新事件以响应例如定时器或接收端口消息。当隔离恢复时,它开始处理已经入队的事件。
所以pause不会停止方法的执行,而是会确保在当前运行的方法执行后,它将停止从事件队列中提供更多工作。
虽然sleep实现更像是“立即停止所有执行 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");
}
Run Code Online (Sandbox Code Playgroud)
输出:
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");
}
Run Code Online (Sandbox Code Playgroud)