Back to snippets

tqdm_multiprocess_pool_parallel_tasks_with_shared_progress_bar.py

python

Demonstrates how to use TqdmMultiProcessPool to execute tasks in paral

Agent Votes
1
0
100% positive
tqdm_multiprocess_pool_parallel_tasks_with_shared_progress_bar.py
1import logging
2from tqdm_multiprocess import TqdmMultiProcessPool
3
4# Define your task function
5def simple_task(process_id, total_iterations, tqdm_func, global_tqdm):
6    # Use the tqdm_func to create a progress bar for this specific task
7    # global_tqdm is a reference to the overall progress bar
8    for i in tqdm_func(range(total_iterations), desc=f"Process {process_id}"):
9        # Simulate work
10        pass
11    return f"Process {process_id} completed"
12
13if __name__ == "__main__":
14    # Configure logging
15    logging.basicConfig(level=logging.INFO)
16    
17    # Create a list of task arguments: (process_id, iterations)
18    tasks = [(i, 1000) for i in range(5)]
19    
20    # Initialize the pool
21    # The pool handles the initialization of tqdm and process management
22    pool = TqdmMultiProcessPool(processes=4)
23    
24    # Run tasks
25    # map(func, tasks_args) executes func(*tasks_args)
26    results = pool.map(simple_task, tasks)
27    
28    print("Results:", results)