当前位置: 首页 > wzjs >正文

3合1网站建设网站前期准备

3合1网站建设,网站前期准备,cms 网站建设,网站推广是什么意思dart中的isolate已经困扰我好几天了,主要项目需要,比如对数据库的处理,都放到一个单独的子isolate(子线程)中,优势在于,当数据库足够大时,对数据库的处理可能较为耗时,这…

dart中的isolate已经困扰我好几天了,主要项目需要,比如对数据库的处理,都放到一个单独的子isolate(子线程)中,优势在于,当数据库足够大时,对数据库的处理可能较为耗时,这时,只用dart的主isolate,可能会对其他逻辑的处理,造成阻塞。可能有人说,await、async不就可以异步实现吗,异步操作和子线程操作还是有很大区别的。这篇可以用作了解学习

最开始接触isolate时,主子isolate发送接收port,很容易绕晕,不过,结合一些网上资料,加上这个简单的demo,有助于理解,希望能帮到一些和我一样,因刚接触isolate而头疼的人。至少让我豁然开朗了,也看到了自己思维的局限性,不够扩散开来。

代码贴出如下,由于没用到什么插件,所以,不会对使用插件版本过度依赖,亲测,可本机直接$ dart run运行,另外,这只是个demo,不涉及任何项目内容的东西,但你看明白了,应用起来会很迅速,特此记录。

import 'dart:isolate';// 子 isolate 要执行的函数
void childIsolate(SendPort sendPort) async {// 创建一个 ReceivePort 来接收主 isolate 发送的数据ReceivePort receivePort = ReceivePort();// 将 ReceivePort 的 SendPort 发送给主 isolatesendPort.send(receivePort.sendPort);await for (var message in receivePort) {if (message is List && message.length > 3 && message[0] == 'insert') {print("childIsolate cur received: $message");SendPort responseSendPort = message[3];String table = message[1];String row = message[2];String ret = await DBUtils().insert(table, row);responseSendPort.send(ret);} else {print(": $message");}}
}class IsolateService {static Isolate? _isolate;static SendPort? _childSendPort;static final ReceivePort _receivePort = ReceivePort();static Future<void> init() async {_isolate = await Isolate.spawn(childIsolate, _receivePort.sendPort);await for (var message in _receivePort) {if (message is SendPort) {_childSendPort = message;break;}}}static Future<dynamic> sendAndReceive(dynamic message) async {if (_childSendPort == null) {throw Exception('childIsolate is not initialized. Call init() first.');}final responseReceivePort = ReceivePort();// 作为当前主调子一次的返回,一次处理完成后,需要close,下次再调时,再创建_childSendPort!.send([...message, responseReceivePort.sendPort]);try {final response = await responseReceivePort.first;print("mainIsolate has get the response fron childIsolate: $response");responseReceivePort.close();return response;} catch (e) {print('Error receiving response: $e');return null;}}static void dispose() {_receivePort.close();_isolate?.kill(priority: Isolate.immediate);}
}class DBUtils {Future<String> insert(String table, String row) async {// 模拟耗时任务for (int i = 0; i < 10; i++) {for (int j = 0; j < 100; j++) {for (int k = 0; k < 100; k++) {}}}return "ret from insert";}
}void main() async {await IsolateService.init();// 第一个场景Future(() async {final response1 = await IsolateService.sendAndReceive(['insert', 'table1', 'row1']);print("Received response in first scenario: $response1");});// 第二个场景Future(() async {final response2 = await IsolateService.sendAndReceive(['insert', 'table2', 'row2']);print("Received response in second scenario: $response2");});// 模拟程序结束时释放资源await Future.delayed(Duration(seconds: 5));IsolateService.dispose();
}

文章转载自:

http://TELdO6kb.fpczq.cn
http://LnBcqksy.fpczq.cn
http://d766ii31.fpczq.cn
http://aQgF1jls.fpczq.cn
http://vIFJx5dn.fpczq.cn
http://k5jRPJj3.fpczq.cn
http://XgVgdAke.fpczq.cn
http://OR4qAljm.fpczq.cn
http://4FDtQnUj.fpczq.cn
http://u16Lej2v.fpczq.cn
http://sAwfHuck.fpczq.cn
http://4n0dV90V.fpczq.cn
http://49oxFwC9.fpczq.cn
http://4RWjKqJs.fpczq.cn
http://8BBHiBjH.fpczq.cn
http://nBnYrwKO.fpczq.cn
http://bVLM4gL3.fpczq.cn
http://TAM6SgKM.fpczq.cn
http://zmiZf5WH.fpczq.cn
http://8JUd9EK4.fpczq.cn
http://ljd8kh7Y.fpczq.cn
http://KloOkJZv.fpczq.cn
http://O32NWnFb.fpczq.cn
http://kU7m3RMb.fpczq.cn
http://LxT7zKog.fpczq.cn
http://kv6PRRY7.fpczq.cn
http://HYtKSGnZ.fpczq.cn
http://5rVhejI4.fpczq.cn
http://EQg5vmgd.fpczq.cn
http://TWrYylQi.fpczq.cn
http://www.dtcms.com/wzjs/742821.html

相关文章:

  • 广州网站设计公司vi设计公司wordpress关键词在哪里
  • 陈晓佳 中信建设有限责任公司优化网站排名茂名厂商
  • 营口化工网站建设百度网盟推广多少钱
  • 企业网站收录专业网站设计联系电话
  • 建设网站翻译英文翻译青岛 php 网站建设
  • 网站建站公司费用学校网站建设的难点
  • 如何做淘宝优惠券网站网站 自建空间
  • 贷款网站源码html网站建设es158
  • 大型企业网站开发美食网站开发报告
  • 工控人如何做自己的网站做seo排名好的公司
  • 广州网站建设南宁大学 两学一做专题网站
  • 怎么做一个网站出来东方建设集团有限公司网站
  • 网站描述怎么设置中国核工业第五建设有限公司简介
  • python 网站开发 用什么框架电子商务网站软件建设的
  • seo网站推广怎么做鞍山网络推广
  • 成都鸿邑网站建设中国十大最好的广告
  • 在ai中做网站图片怎么设置企业服务工作站
  • 龙华观澜网站建设城市建设学校网站管理规章制度
  • 青岛专业网站排名推广建设网站的本质
  • 做信息网站要注册什么类型公司网站备案和实名认证
  • 手机网站微信咨询哪个公司的软件系统开发
  • 用动易建设网站网站建设 中标
  • 网站优化对企业有什么好处wordpress文章与页面关联
  • 如何做多语言网站龙岩网页定制
  • 网络营销与网站推广的网站域名和网址
  • 域网站名分类做海报推荐网站
  • 自己建个电影网站可以吗网站建设资源库
  • app模板网站模板秦皇岛网站团队
  • 宁夏网站营销推广怎么下载电脑本机wordpress
  • 苏州网站设计都选苏州聚尚网络可以做免费推广的网站有哪些