使用Asyncio的TQDM进度栏
我正在尝试创建一个单个进度栏,每当完成异步任务时,该栏将进行更新。
我有以下代码上述
scan_results = []
tasks = [self.run_scan(i) for i in input_paths]
pbar = tqdm(total=len(tasks), desc='Scanning files')
for f in asyncio.as_completed(tasks):
value = await f
pbar.update(1)
scan_results.append(value)
代码生成一个单个进度栏,但是直到所有任务完成(在一个任务中显示为0%或100%)之前,它才会更新,而单个任务超过一个任务)
也尝试使用tqdm。 asyncio.tqdm.gather
with tqdm(total=len(input_paths)):
scan_results = await tqdm.gather(*[self.run_scan(i) for i in input_paths])
以上代码生成多个进度条,就像在上一个代码块中一样,它显示为0%或100%
我的起点是,我的起点是
scan_results = await asyncio.gather(*[self.run_scan(i)for i in input_paths])
您可以帮助您与单个和动态进度一起使用的帮助酒吧
I am trying to create a single progress bar that will be updated whenever an async task is done.
I have the following code
scan_results = []
tasks = [self.run_scan(i) for i in input_paths]
pbar = tqdm(total=len(tasks), desc='Scanning files')
for f in asyncio.as_completed(tasks):
value = await f
pbar.update(1)
scan_results.append(value)
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
with tqdm(total=len(input_paths)):
scan_results = await tqdm.gather(*[self.run_scan(i) for i in input_paths])
The above code generates multiple progress bars and as in the previous code block, it shows either 0% or 100%
My starting point was
scan_results = await asyncio.gather(*[self.run_scan(i)for i in input_paths])
Would appreciate your help on making it work with a single and dynamic progress bar
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您调用
self.pbar.update(1)
在run_scan
扫描方法中创建并发任务后,每个任务将更新pbar
for self 。所以你的班级应该看起来如下If you call
self.pbar.update(1)
inside therun_scan
scan method after creating concurrent tasks, each task will update thepbar
for self. So your class should look like the following