3

I'm having trouble figuring out how to properly nest tqdm in a loop. I'm trying to get the progress of aria2 download through xmlrpc interface. tqdm is able to read the progress, however with every update it puts it on new line therefore spamming the terminal. Here's the code:

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

and what that gives is:

 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]

For now, I'm just using the first aria2 download hence the activeconnections[0], but eventually the goal is to spawn a progressbar for every download that is in xmlrpc response list.

Thanks so much.

kokozz
  • 37
  • 6

0 Answers0