V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
939224390
V2EX  ›  问与答

aria2 在 Flutter 应用中卡住的原因及解决方案?

  •  
  •   939224390 · 72 天前 · 811 次点击
    这是一个创建于 72 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我想用 Flutter 做一个 aria2 的下载器,在 aria2 文档中,我看到可以使用 http 或者 websocket 发送请求。

    我使用 http ,发送 post 请求,aria2 版本是 1.37.0 ,发现无论发不发送请求,第十秒之后发送请求必定卡住,于是我换了一个 aria 的修改版,这个版本不会阻塞在第十秒,但是会卡在下载任务上,浏览器使用了 aria2 Explorer ,发送下载任务就直接卡住,后面问了一下 AI ,说可以修改一下 conf ,修改之后发送下载任务不会卡住,但会在下载过程中卡住。我在 aria2 日志中并没有发现卡住以及卡住之后的请求。Dio 也是一样

    但是卡住之后,重启程序就会恢复正常,aria2 的下载任务会继续下载,再次发送下载任务也不会卡住。

    使用 websocket 也是一样会卡住。

    我使用了 bun axios 发送 post 请求就不会卡住,但是使用 bun websocket 则无法连接,询问 AI ,告诉我说 aria2 并不支持 websocket ,而是 flutter 的 websocket 降级成了 http 。

    // aria2.conf
    file-allocation=none
    enable-direct-io=true
    
    rpc-max-request-size=2M
    rpc-save-upload-metadata=true
    
    disk-cache=16M
    file-allocation=trunc
    
    //dart
    final client = http.Client();
    final response = await client
              .post(
                Uri.parse(aria2Url),
                headers: {'Content-Type': 'application/json'},
                body: jsonEncode({
                  'jsonrpc': '2.0',
                  'method': method,
                  'params': params,
                  'id': '1',
                }),
              )
              .timeout(Duration(seconds: 1));
    print(response.body);
    client.close();
    

    使用过 http 、Dio 、web_socket_channel(+json_rpc_2)等包发送请求均会卡住。 询问 AI 后,说 flutter 的 http 会 keep alive ,发送太多请求可能会阻塞,于是我在每次发送请求的时候都新建 client ,收到请求后 close ,但是并没有效果。 加入 timeout 之后,卡住的请求和之后的请求都会超时,但 aria2 仍旧一直处在卡住的状态。

    5 条回复    2025-07-08 16:40:55 +08:00
    jifengg
        1
    jifengg  
       71 天前
    楼主,问你几个问题:
    - 你用 flutter 开发,测试是用什么端? android ? windows ?
    - aria2 是运行在什么环境?
    - “卡住”是指的具体什么?
    - 如果这个下载器和 aria2 是分开部署的,那么,抛弃下载器,curl 发送 aria2 请求,会有“卡住”现象吗?
    939224390
        2
    939224390  
    OP
       70 天前
    @jifengg 我在 Windows11 开发的,aria2 也是运行在本地
    卡住的时候 我看 aria2 日志也不会更新了,aria2 Explorer 也无法链接进去,下载任务也卡住了,等我关掉程序之后,aria2 Explorer 链接恢复,下载任务继续。

    aria2Process = await Process.start(aria2Path, [
    '--dir=${config.savePath}',
    '--max-concurrent-downloads=${config.maxDown}',
    '--max-connection-per-server=${config.threadCount}',
    '--conf-path=$aria2ConfPath',
    '--rpc-listen-port=16800',
    '--save-session-interval=60',
    '--continue=true',
    '--log=$aria2LogPath',
    '--log-level=debug',
    ]);



    enable-rpc=true

    rpc-allow-origin-all=true

    rpc-listen-all=true

    file-allocation=none
    enable-direct-io=true

    rpc-max-request-size=10M
    rpc-save-upload-metadata=true
    min-split-size=1M

    disk-cache=16M
    file-allocation=trunc
    jifengg
        3
    jifengg  
       70 天前
    一个猜测:你用 Dart 启动了 aria2 进程,但是没有监听它的输出流,aria2 的 stdout 缓存满了,柱塞了整个 aria2 进程。
    939224390
        4
    939224390  
    OP
       70 天前
    @jifengg 试了试好像还真是这个原因,🙏
    jifengg
        5
    jifengg  
       70 天前
    @939224390 找到原因了就好。楼主可以试试再继续优化这个问题。
    如果可以,欢迎后续把你的优化继续回复到这个帖子里,方便后来人。
    关于   ·   帮助文档   ·   自助推广系统   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1075 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 22:47 · PVG 06:47 · LAX 15:47 · JFK 18:47
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.