感觉不我之前写的方法确实是有点问题 于是研究了半天找了一个插件
1.添加依赖:
worker_manager
2.main函数在runApp之前先初始化 warmup函数也可以自定义设置线程池数 isolate数
3.正文
//算法2
Executor executor= Executor();
while(i<length-1){
String mid=idList[i];
String client=clientList[i];
i++;
String mainUrl='https://maplestory.io/api/${client}map';
String url = '${mainUrl}/${mid}/bgm';
List params=[file,url,mid];
// print('正在下载第$i/${length}个Map映射url ${mid}->${client} 总线程数:${currentThreadNum}/${maxThreadNum} ');
executor.execute(arg1:params,fun1: doComputeWriteRedirectUrl).next(
onValue: (value) async {
await Future.delayed(Duration(microseconds:(Random().nextDouble()*1000*100).floor()));
taskIndex++;
print('完成下载第$taskIndex/${length}个Map映射url ${mid}->${client} ');
if(taskIndex==length){
print('任务完成 $taskIndex/${length}');
}
//currentThreadNum--;
},
onError: (error) async {
print(error);
print('错误下载第$taskIndex/${length}个Map映射url ${mid}->${client}');
await Future.delayed(Duration(microseconds:(Random().nextDouble()*1000*100).floor()));
//currentThreadNum--;
}
);
}
//endregion
print('任务分配结束');
改良x2
onvalue异步会出现同时写入文本的情况 导致写入的时候部分文本不完整 导致严重错误
Executor().execute(
arg1:params,
fun1: doComputeWriteRedirectUrl
).next(
onValue: (resStr){
if(!resStr.toString().contains('=')){
print('一个出错 ${resStr}');
}
//compute(doComputeWriteEa,[resStr,file]);
//等待时间防止同时?
//await Future.delayed(Duration(microseconds:(Random().nextDouble()*1000*100).floor()));
//Executor().execute(priority: WorkPriority.high,arg1:[resStr,file],fun1:doComputeWriteEa );
doComputeWriteEa([resStr,file]);
//好像不设置异步能解决这个问题
taskIndex++;
print('完成下载第$taskIndex/${length}个Map映射url ${mid}->${client} ${(resStr as String ).replaceAll('\n', '')}');
if(taskIndex==length){
print('任务完成 $taskIndex/${length}');
}
//currentThreadNum--;
},
onError: (error) async {
print(error);
print('错误下载第$taskIndex/${length}个Map映射url ${mid}->${client}');
await Future.delayed(Duration(microseconds:(Random().nextDouble()*1000*100).floor()));
//currentThreadNum--;
}
);