Flutter 创建 isolate 耗时过长应该如何解决呢?
问题
- 当我执行isolate.spawn这个方法去创建一个隔离的时候,经常会遇到方法需要长时间等待,这是为什么呢?
- 如何才能快速得到isolate.spwan建立的isolate呢?
- 我尝试在传递的方法
command.params['method']
中计时,发现他的计算非常快,也就十几毫秒。 - 当我在VScode中调试代码的时候,长时间等待的时候,通过暂停线程、重新启用线程的操作后,isolate会快速得到执行,让我很是怀疑,这个线程在创建的时候,被挂起了。
- 我该如何更快的确保isolate.spawn得到快速执行,并获取其返回值呢?
Isolate.spawn调用的静态方法与参数
static startSpo2DeviceParse(IsolateCommandParams sender) { return Spo2DeviceParseOption( sender.sender, device: sender.params['device'], caseStateInfo: sender.params['caseinfo'], ); }
Isolate.spawn创建过程
int begin = DateTime.now().millisecondsSinceEpoch; Isolate isolate = await Isolate.spawn(command.params['method'], argparams, debugName: command.params['token'], errorsAreFatal: true, onError: _onError); print( "Isolate.spawn ${command.params['token']} complete:cost time:${DateTime.now().millisecondsSinceEpoch - begin}");
Isolate.spawn执行结果
I/flutter (29746): Isolate.spawn parsecore complete:cost time:5882 I/flutter (29746): Isolate.spawn hubCore complete:cost time:5911 I/flutter (29746): Isolate.spawn preparse complete:cost time:2071 I/flutter (29746): Isolate.spawn preparse complete:cost time:2549