3

我无法弄清楚如何正确地将 tqdm 嵌套在一个循环中。我正在尝试通过xmlrpc接口获取aria2下载的进度。tqdm 能够读取进度,但是每次更新都会将其放在新行上,因此会向终端发送垃圾邮件。这是代码:

s = xmlrpc.client.ServerProxy("http://localhost:6800/rpc")

r = s.aria2.addUri(["http://ipv4.download.thinkbroadband.com/100MB.zip"],
                   {"dir": "/Downloads/ariatest"})

while True:
    globalstat = s.aria2.getGlobalStat()
    activeglobal = int(globalstat['numActive'])
    activeconnections = s.aria2.tellActive()

    if activeglobal > 0:
        totalLength = int(activeconnections[0]['totalLength'])
        completedLength = int(activeconnections[0]['completedLength'])
        with tqdm(total=totalLength, unit='B', unit_scale=True, unit_divisor=1024, leave=True) as pbar:
            pbar.update(completedLength)
            time.sleep(0.1)
    else:
        print('all done')
        break

那给出的是:

 26%|██▌       | 6.38M/25.0M [00:00<00:00, 890GB/s]
 26%|██▌       | 6.42M/25.0M [00:00<00:00, 843GB/s]
 26%|██▌       | 6.46M/25.0M [00:00<00:00, 874GB/s]
 26%|██▌       | 6.48M/25.0M [00:00<00:00, 877GB/s]
 26%|██▌       | 6.51M/25.0M [00:00<00:00, 597GB/s]
 26%|██▌       | 6.54M/25.0M [00:00<00:00, 654GB/s]
 26%|██▋       | 6.58M/25.0M [00:00<00:00, 839GB/s]
 26%|██▋       | 6.62M/25.0M [00:00<00:00, 963GB/s]
 27%|██▋       | 6.67M/25.0M [00:00<00:00, 931GB/s]

目前,我只使用第一个 aria2 下载,因此使用了 activeconnections[0],但最终目标是为 xmlrpc 响应列表中的每个下载生成一个进度条。

非常感谢。

4

0 回答 0