I am trying to create a single progress bar that will be updated whenever an async task is done.
I have the following code
JavaScript
x
10
10
1
scan_results = []
2
tasks = [self.run_scan(i) for i in input_paths]
3
4
pbar = tqdm(total=len(tasks), desc='Scanning files')
5
6
for f in asyncio.as_completed(tasks):
7
value = await f
8
pbar.update(1)
9
scan_results.append(value)
10
The above code generates a single progress bar as but it is not updated until all tasks are finished (it shows either 0% or 100% while there’s more than a single task)
I also tried using tqdm.asyncio.tqdm.gather
JavaScript
1
3
1
with tqdm(total=len(input_paths)):
2
scan_results = await tqdm.gather(*[self.run_scan(i) for i in input_paths])
3
The above code generates multiple progress bars and as in the previous code block, it shows either 0% or 100%
My starting point was
JavaScript
1
2
1
scan_results = await asyncio.gather(*[self.run_scan(i)for i in input_paths])
2
Would appreciate your help on making it work with a single and dynamic progress bar
Advertisement
Answer
If you call self.pbar.update(1)
inside the run_scan
scan method after creating concurrent tasks, each task will update the pbar
for self. So your class should look like the following
JavaScript
1
12
12
1
class Cls:
2
async def run_scan(self, path):
3
4
self.pbar.update(1)
5
6
def scan(self, input_paths):
7
loop = asyncio.get_event_loop()
8
tasks = [loop.create_task(self.run_scan(i)) for i in input_paths]
9
self.pbar = tqdm(total=len(input_paths), desc='Scanning files')
10
loop.run_until_complete(asyncio.gather(*tasks))
11
loop.close()
12