[英]Access variables in external scope when using isolation in Dart
在 Isolates 中,我可以引用外部 scope 的局部变量或 class 的字段变量,而无需将其作为单独的消息传递。
这是将值隐式复制到新隔离的 memory 区域吗?
我很好奇细节。
例子
class Person {
Person(this._baseNum);
/// access [_baseNum] in isolate
final int _baseNum;
int age = 0;
/// access [extraAge] in isolate
Future<void> addAge(int extraAge) async {
final mainReceivePort = ReceivePort();
await Isolate.spawn((SendPort sendPort) async {
sendPort.send(await _calcAge(_baseNum, extraAge));
}, mainReceivePort.sendPort);
age = await mainReceivePort.first;
mainReceivePort.close();
}
static Future<int> _calcAge(int someNum, int age) async {
// ... heavy work ...
return age + someNum;
}
}
// ...
void main() {
test('test', () async {
final p = Person(10);
await p.addAge(3);
expect(p.age, 13);
});
}
在 Isolates 中,我可以引用外部 scope 的局部变量或 class 的字段变量,而无需将其作为单独的消息传递。
这是将值隐式复制到新隔离的 memory 区域吗?
是的。
证明这一点的一种方法是,如果您从外部 scope 或字段变量中获取这些变量之一,并更新隔离中的值。 您将看到,从隔离外部看,该值不会更新。 这是因为他们正在处理变量的独立副本。
import 'dart:isolate';
import 'package:test/test.dart';
class Person {
Person(this._baseNum);
/// access [_baseNum] in isolate
int _baseNum;
int age = 0;
/// access [extraAge] in isolate
Future<void> addAge(int extraAge) async {
final mainReceivePort = ReceivePort();
await Isolate.spawn((SendPort sendPort) async {
_baseNum++; // modify _baseNum
sendPort.send(await _calcAge(_baseNum, extraAge));
}, mainReceivePort.sendPort);
age = await mainReceivePort.first;
mainReceivePort.close();
}
static Future<int> _calcAge(int someNum, int age) async {
// ... heavy work ...
return age + someNum;
}
}
// ...
void main() {
test('test', () async {
final p = Person(10);
await p.addAge(3);
expect(p.age, 14);
expect(p._baseNum, 10); // _baseNum still 10 despite _baseNum++ in isolate
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.