Created
January 13, 2022 23:04
-
-
Save Crazzy626/6c60d598cd05f0fe6e70bd3011958f7d to your computer and use it in GitHub Desktop.
Progress bar with asyncio and tqdm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Display a Progress Bar while running tasks with asyncio | |
""" | |
import asyncio | |
import tqdm.asyncio | |
from tqdm.auto import tqdm | |
async def run_multiple_jobs_async(job_list): | |
tasks = [] | |
for a_job in job_list: | |
task = asyncio.create_task(do_job(a_job)) | |
tasks.append(task) | |
tot_tasks = len(tasks) | |
for f in tqdm(asyncio.as_completed(tasks), total=tot_tasks, desc="Jobs Progress: ", unit=" job"): | |
await f # Returns a single response and updating progress bar | |
async do_job(data): | |
# Long running TASK and generate job_result | |
return job_result | |
if __name__ == '__main__': | |
job_list = [data_for_job1, data_for_job2, data_for_job3] # <define_data_for_every_job> | |
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) | |
asyncio.run(run_multiple_jobs_async(job_list)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment