一次处理大量的HTTP请求

问题描述

要求:

我有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。这可能是因为我的移动设备上存在内存问题,因为它在Web平台上可以正常工作。

我尝试了其他解决方案,例如使用建议hereFuture.wait()解决方案,但该解决方案无法正常工作且设备已挂起。

此方案的最佳解决方案是什么?

最糟糕的解决方案:

使用等待关键字。完全没有例外,但是整个过程花费了太长时间。

for(String url in urls) {
    final bytes = await bytesForFileAtUrl(url);
    ...
    ...
}

解决方法

Error: nanodbc/nanodbc.cpp:983: FA004: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Failed to authenticate the user 'my_user_id' in Active Directory (Authentication option is 'ActiveDirectoryPassword').
Error code 0x800401F0; state 10
CoInitialize has not been called. 

也许试试这个?

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...