1.1. 第一步:创建并握手

如前所述,Isolate 不共享任何内存并通过消息进行交互,因此,我们需要找到一种方法在「调用者」与新的 isolate 之间建立通信。

每个 Isolate 都暴露了一个将消息传递给 Isolate 的被称为「SendPort」的端口。(个人觉得该名字有一些误导,因为它是一个接收/监听的端口,但这毕竟是官方名称)。

这意味着「调用者」和「新的 isolate」需要互相知道彼此的端口才能进行通信。这个握手的过程如下所示:

// 新的 isolate 端口
// 该端口将在未来使用
// 用来给 isolate 发送消息
//
SendPort newIsolateSendPort; //
// 新 Isolate 实例
//
Isolate newIsolate; //
// 启动一个新的 isolate
// 然后开始第一次握手
//
//
void callerCreateIsolate() async {
//
// 本地临时 ReceivePort
// 用于检索新的 isolate 的 SendPort
//
ReceivePort receivePort = ReceivePort(); //
// 初始化新的 isolate
//
newIsolate = await Isolate.spawn(
callbackFunction,
receivePort.sendPort,
); //
// 检索要用于进一步通信的端口
//
//
newIsolateSendPort = await receivePort.first;
} //
// 新 isolate 的入口
//
static void callbackFunction(SendPort callerSendPort){
//
// 一个 SendPort 实例,用来接收来自调用者的消息
//
//
ReceivePort newIsolateReceivePort = ReceivePort(); //
// 向调用者提供此 isolate 的 SendPort 引用
//
callerSendPort.send(newIsolateReceivePort.sendPort); //
// 进一步流程
//
}

  

1.2. 第二步:向 Isolate 提交消息

现在我们有了向 Isolate 发送消息的端口,让我们看看如何做到这一点:

//
// 向新 isolate 发送消息并接收回复的方法
//
//
// 在该例中,我将使用字符串进行通信操作
// (发送和接收的数据)
//
Future<String> sendReceive(String messageToBeSent) async {
//
// 创建一个临时端口来接收回复
//
ReceivePort port = ReceivePort(); //
// 发送消息到 Isolate,并且
// 通知该 isolate 哪个端口是用来提供
// 回复的
//
newIsolateSendPort.send(
CrossIsolatesMessage<String>(
sender: port.sendPort,
message: messageToBeSent,
)
); //
// 等待回复并返回
//
return port.first;
} //
// 扩展回调函数来处理接输入报文
//
static void callbackFunction(SendPort callerSendPort){
//
// 初始化一个 SendPort 来接收来自调用者的消息
//
//
ReceivePort newIsolateReceivePort = ReceivePort(); //
// 向调用者提供该 isolate 的 SendPort 引用
//
callerSendPort.send(newIsolateReceivePort.sendPort); //
// 监听输入报文、处理并提供回复的
// Isolate 主程序
//
newIsolateReceivePort.listen((dynamic message){
CrossIsolatesMessage incomingMessage = message as CrossIsolatesMessage; //
// 处理消息
//
String newMessage = "complemented string " + incomingMessage.message; //
// 发送处理的结果
//
incomingMessage.sender.send(newMessage);
});
} //
// 帮助类
//
class CrossIsolatesMessage<T> {
final SendPort sender;
final T message; CrossIsolatesMessage({
@required this.sender,
this.message,
});
}

  

1.3. 第三步:销毁这个新的 Isolate 实例

当你不再需要这个新的 Isolate 实例时,最好通过以下方法释放它:

//
// 释放一个 isolate 的例程
//
void dispose(){
newIsolate?.kill(priority: Isolate.immediate);
newIsolate = null;
}

  

1.4. 特别说明 - 单监听器流

你可能已经注意到我们正在使用流在「调用者」和新 isolate 之间进行通信。这些流的类型为:「单监听器」流。

2. 一次性计算

如果你只需要运行一些代码来完成一些特定的工作,并且在工作完成之后不需要与 Isolate 进行交互,那么这里有一个非常方便的称为 compute 的 Helper。

主要包含以下功能:

  • 产生一个 Isolate,
  • 在该 isolate 上运行一个回调函数,并传递一些数据,
  • 返回回调函数的处理结果,
  • 回调执行后终止 Isolate。

isolate两三事的更多相关文章

  1. [Erlang 0124] Erlang Unicode 两三事 - 补遗

    最近看了Erlang User Conference 2013上patrik分享的BRING UNICODE TO ERLANG!视频,这个分享很好的梳理了Erlang Unicode相关的问题,基本 ...

  2. AngularJS 全局scope与Isolate scope通信

    在项目开发时,全局scope 和 directive本地scope使用范围不够清晰,全局scope与directive本地scope通信掌握的不够透彻,这里对全局scope 和 directive本地 ...

  3. [AngularJS] Isolate State Mutations in Angular Components

    Managing state is one of the hardest things to do in any application. Angular 2 tackles this problem ...

  4. 使用Go语言两三事

    使用Go语言两三事,在网上看到的总结的很不错哦,转自http://www.cnblogs.com/sevenyuan/archive/2013/02/27/2935887.html 一.channel ...

  5. 全局scope与Isolate scope通信

    AngularJS 全局scope与Isolate scope通信 在项目开发时,全局scope 和 directive本地scope使用范围不够清晰,全局scope与directive本地scope ...

  6. isolate demo

    dependencies dependencies: flutter: sdk: flutter # The following adds the Cupertino Icons font to yo ...

  7. Dart - Isolate 并发

    在Dart中实现并发可以用Isolate,它是类似于线程(thread)但不共享内存的独立运行的worker,是一个独立的Dart程序执行环境.其实默认环境就是一个main isolate. 在Dar ...

  8. 理解CSS3 isolation: isolate的表现和作用

    转自:http://www.zhangxinxu.com/wordpress/?p=5155 只要元素可以创建层叠上下文,就可以阻断mix-blend-mode! 于是,不仅仅是isolation:i ...

  9. mix-blend-mode 混合模式 background-blend-mode 背景混合模式 isolation:isolate 隔离

    css3 mix-blend-mode 混合模式 该属性不仅可以作用于HTML,还可以作用于SVG 兼容性: IE 8~11 Edge 12~14 Firefox 41~47 chrome 45~51 ...

随机推荐

  1. Windows上node.js的多版本管理工具

    在Linux上我一直使用nvm来管理nodejs的不同版本,但是nvm没有windows版本,今天发现在windows上可以使用另外一个版本管理工具nvm-windows来管理. 下载与安装下载地址: ...

  2. OpenFOAM——圆腔顶盖旋转驱流

    本算例来自<ANSYS Fluid Dynamics Verification Manual>中的VMFL008: Flow Inside a Rotating Cavity 腔体顶盖以1 ...

  3. ubuntu之路——day17.1 卷积操作的意义、边缘检测的示例、filter与padding的关系、卷积步长

    感谢吴恩达老师的公开课,以下图片均来自于吴恩达老师的公开课课件 为什么要进行卷积操作? 我们通过前几天的实验已经做了64*64大小的猫图片的识别. 在普通的神经网络上我们在输入层上输入的数据X的维数为 ...

  4. 【Alpha】“北航社团帮”小程序v1.0项目展示

    目录 1.团队介绍 2.回答一些工程问题 整个项目的目标和预期功能 整个项目的预期典型用户 整个项目的预期用户数量 alpha满足的用户需求 alpha用户量一览 团队分工及经验教训 团队项目管理 时 ...

  5. Servlet使用反射机制

    传统servlet存在的问题 每一个不同的请求都要写Servlet,导致整个项目servlet过多,不易维护 解决方案 同一个模块只写一个Servlet,然后每一个请求传一个参数,后台根据参数取调用不 ...

  6. RUN vs CMD vs ENTRYPOINT

    参考:https://www.ibm.com/developerworks/community/blogs/132cfa78-44b0-4376-85d0-d3096cd30d3f/entry/RUN ...

  7. 正则表达式在线分析 regex online analyzer

    https://regexper.com/#%2F%5B0-9%5D%5Cs%5B0-9%5D%2F https://regexper.com/ http://regexone.com/lesson/ ...

  8. arcpy实例教程-地图范围导出到要素类

    arcpy实例教程-地图范围导出到要素类 商务合作,科技咨询,版权转让:向日葵,135-4855_4328,xiexiaokui#qq.com 功能:将当前地图范围导出到内存要素类 描述:将当前地图的 ...

  9. postgres开启慢查询日志

    1.全局设置修改配置postgres.conf: log_min_duration_statement=5000 然后加载配置: postgres=# select pg_reload_conf() ...

  10. Python实现PIL将图片转成字符串

    # -*- coding: utf-8 -*- # author:baoshan from PIL import Image, ImageFilter codeLib = '''@#$%&?* ...