{"id":19428873,"url":"https://github.com/eleutherai/tqdm-multiprocess","last_synced_at":"2025-04-24T18:31:31.423Z","repository":{"id":62585081,"uuid":"298521627","full_name":"EleutherAI/tqdm-multiprocess","owner":"EleutherAI","description":"Using queues, tqdm-multiprocess supports multiple worker processes, each with multiple tqdm progress bars, displaying them cleanly through the main process. It offers similar functionality for python logging. ","archived":false,"fork":false,"pushed_at":"2021-01-06T22:37:43.000Z","size":50,"stargazers_count":41,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-09-20T08:35:11.395Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/EleutherAI.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-09-25T09:01:27.000Z","updated_at":"2024-06-14T13:10:29.000Z","dependencies_parsed_at":"2022-11-03T22:04:28.487Z","dependency_job_id":null,"html_url":"https://github.com/EleutherAI/tqdm-multiprocess","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EleutherAI%2Ftqdm-multiprocess","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EleutherAI%2Ftqdm-multiprocess/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EleutherAI%2Ftqdm-multiprocess/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EleutherAI%2Ftqdm-multiprocess/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/EleutherAI","download_url":"https://codeload.github.com/EleutherAI/tqdm-multiprocess/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223961111,"owners_count":17232251,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-10T14:17:02.914Z","updated_at":"2024-11-10T14:17:03.465Z","avatar_url":"https://github.com/EleutherAI.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# tqdm-multiprocess\nUsing queues, tqdm-multiprocess supports multiple worker processes, each with multiple tqdm progress bars, displaying them cleanly through the main process. The worker processes also have access to a single global tqdm for aggregate progress monitoring.\n\nLogging is also redirected from the subprocesses to the root logger in the main process.\n\nCurrently doesn't support tqdm(iterator), you will need to intialize your worker tqdms with a total and update manually.\n\nDue to the performance limits of the default Python multiprocess queue you need to update your global and worker process tqdms infrequently to avoid flooding the main process. I will attempt to implement a lock free ringbuffer at some point to see if things can be improved.\n\n## Installation\n\n```bash\npip install tqdm-multiprocess\n```\n\n## Usage\n\n*TqdmMultiProcessPool* creates a standard python multiprocessing pool with the desired number of processes. Under the hood it uses async_apply with an event loop to monitor a tqdm and logging queue, allowing the worker processes to redirect both their tqdm objects and logging messages to your main process. There is also a queue for the workers to update the single global tqdm.\n\nAs shown below, you create a list of tasks containing their function and a tuple with your parameters. The functions you pass in will need the extra arguments on the end \"tqdm_func, global_tqdm\". You must use tqdm_func when initializing your tqdms for the redirection to work. As mentioned above, passing iterators into the tqdm function is currently not supported, so set total=total_steps when setting up your tqdm, and then update the progress manually with the update() method. All other arguments to tqdm should work fine.\n\nOnce you have your task list, call the map() method on your pool, passing in the process count, global_tqdm (or None), task list, as well as error and done callback functions. The error callback will be trigerred if your task functions return anything evaluating as False (if not task_result in the source code). The done callback will be called when the task succesfully completes.\n\nThe map method returns a list containing the returned results for all your tasks in original order.\n\n### examples/basic_example.py\n\n```python\nfrom time import sleep\nimport multiprocessing\nimport tqdm\n\nimport logging\nfrom tqdm_multiprocess.logger import setup_logger_tqdm\nlogger = logging.getLogger(__name__)\n\nfrom tqdm_multiprocess import TqdmMultiProcessPool\n\niterations1 = 100\niterations2 = 5\niterations3 = 2\ndef some_other_function(tqdm_func, global_tqdm):\n    \n    total_iterations = iterations1 * iterations2 * iterations3\n    with tqdm_func(total=total_iterations, dynamic_ncols=True) as progress3:\n        progress3.set_description(\"outer\")\n        for i in range(iterations3):\n            logger.info(\"outer\")\n            total_iterations = iterations1 * iterations2\n            with tqdm_func(total=total_iterations, dynamic_ncols=True) as progress2:\n                progress2.set_description(\"middle\")\n                for j in range(iterations2):\n                    logger.info(\"middle\")\n                    #for k in tqdm_func(range(iterations1), dynamic_ncols=True, desc=\"inner\"):\n                    with tqdm_func(total=iterations1, dynamic_ncols=True) as progress1:\n                        for j in range(iterations1):\n                            # logger.info(\"inner\") # Spam slows down tqdm too much\n                            progress1.set_description(\"inner\")\n                            sleep(0.01)\n                            progress1.update()\n                            progress2.update()\n                            progress3.update()\n                            global_tqdm.update()\n\n    logger.warning(f\"Warning test message. {multiprocessing.current_process().name}\")\n    logger.error(f\"Error test message. {multiprocessing.current_process().name}\")\n\n        \n# Multiprocessed\ndef example_multiprocessing_function(some_input, tqdm_func, global_tqdm):  \n    logger.debug(f\"Debug test message - I won't show up in console. {multiprocessing.current_process().name}\")\n    logger.info(f\"Info test message. {multiprocessing.current_process().name}\")\n    some_other_function(tqdm_func, global_tqdm)\n    return True\n\ndef error_callback(result):\n    print(\"Error!\")\n\ndef done_callback(result):\n    print(\"Done. Result: \", result)\n\ndef example():\n    process_count = 4    \n    pool = TqdmMultiProcessPool(process_count)\n\n    task_count = 10\n    initial_tasks = [(example_multiprocessing_function, (i,)) for i in range(task_count)]    \n    total_iterations = iterations1 * iterations2 * iterations3 * task_count\n    with tqdm.tqdm(total=total_iterations, dynamic_ncols=True) as global_progress:\n        global_progress.set_description(\"global\")\n        results = pool.map(global_progress, initial_tasks, error_callback, done_callback)\n        print(results)\n\nif __name__ == '__main__':\n    logfile_path = \"tqdm_multiprocessing_example.log\"\n    setup_logger_tqdm(logfile_path) # Logger will write messages using tqdm.write\n    example() \n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feleutherai%2Ftqdm-multiprocess","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Feleutherai%2Ftqdm-multiprocess","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feleutherai%2Ftqdm-multiprocess/lists"}