【发布时间】:2021-02-11 17:47:36
【问题描述】:
要求:
我有 1000 个 PDF 的 URL,我想从中创建 zip。
功能:
static Future<bool> downloadZip(List<String> urls) {
int counter = 0
for(String url in urls) {
bytesForFileAtUrl(url).then((bytes){
counter++;
if(bytes != null) {
//Add bytes to the zip encoder
}
if(counter == urls.length) {
//close the zip encoder
//download the zip file
}
}
}
}
static Future<Uint8List> bytesForFileAtUrl(String url) async {
try {
http.Response response = await http.get(url);
return response?.bodyBytes;
} catch (e) {
print('Error getting bytes: ${e.toString()}');
return null;
}
}
问题:
当请求数量较少时,它可以正常工作。但如果有大量请求,我会收到异常:SocketException: Connection failed (OS Error: Too many open files, errno = 24) 报告为here。这可能是因为我的移动设备上的内存问题,因为它在网络平台上运行良好。
我尝试了其他解决方案,例如使用 Future.wait() 建议的 here 的解决方案,但它不起作用并且设备被挂起。
这种情况的最佳解决方案是什么?
最坏的解决方案:
使用 await 关键字。一点也不例外,但是整个过程需要很长时间才能完成。
for(String url in urls) {
final bytes = await bytesForFileAtUrl(url);
...
...
}
【问题讨论】:
-
试试
Future.forEach -
@pskink 有什么例子吗?
-
var list = [ [0, 5, 10], [1, 6, 11], [2, 7], [3, 8], [4, 9], ]; var futures = list.map((subList) { return Future.forEach(subList, (i) => Future.delayed(3.seconds, () => print(i))); }); Future.wait(futures).then((_) { print('all done!!!'); });这里最多完成 5 个并发作业
标签: flutter http request simultaneous