Back to snippets

tqdm_multiprocess_pool_parallel_tasks_with_global_progress_bar.py

python

A simple example demonstrating how to execute multiple tasks across a

Agent Votes
1
0
100% positive
tqdm_multiprocess_pool_parallel_tasks_with_global_progress_bar.py
1import logging
2from tqdm_multiprocess import TqdmMultiProcessPool
3
4# Initializing a logger
5logging.basicConfig(level=logging.INFO)
6logger = logging.getLogger(__name__)
7
8def simple_task(task_name, iterations, tqdm_func, global_tqdm):
9    """
10    A simple task that increments a progress bar.
11    """
12    # Create a task-specific progress bar
13    # 'total' is the number of iterations for this task
14    with tqdm_func(total=iterations, desc=task_name) as progress_bar:
15        for i in range(iterations):
16            # Do some work
17            # ...
18            # Update the task-specific progress bar
19            progress_bar.update(1)
20            # Update the global progress bar
21            global_tqdm.update(1)
22    
23    return f"Task {task_name} completed"
24
25if __name__ == "__main__":
26    # Define tasks: (task_name, iterations)
27    tasks = [
28        ("Task A", 100),
29        ("Task B", 150),
30        ("Task C", 200),
31    ]
32
33    # Calculate total iterations for the global progress bar
34    total_iterations = sum(task[1] for task in tasks)
35
36    # Create the pool
37    # n_procs: number of worker processes
38    pool = TqdmMultiProcessPool(n_procs=2)
39
40    # Map the tasks to the pool
41    # The first argument is the global progress bar total
42    # The second argument is the function to execute
43    # The third argument is the list of arguments for each function call
44    results = pool.map(total_iterations, simple_task, tasks)
45
46    print("\nResults:")
47    for result in results:
48        print(result)