| 1 | """ |
| 2 | A global store, for values that can be mutated in multiprocessing, along with their related values. |
| 3 | """ |
| 4 | |
| 5 | import multiprocessing |
| 6 | |
| 7 | # to be set at beginning |
| 8 | total_num_tasks = 0 |
| 9 | num_completed_tasks = multiprocessing.Value("i", 0) |
| 10 | |
| 11 | |
| 12 | # to be set at beginning |
| 13 | total_num_task_groups = 0 |
| 14 | num_completed_task_groups = multiprocessing.Value("i", 0) |
| 15 | |
| 16 | |
| 17 | def init_total_num_tasks(n: int): |
| 18 | global total_num_tasks |
| 19 | total_num_tasks = n |
| 20 | |
| 21 | |
| 22 | def init_total_num_task_groups(n: int): |
| 23 | global total_num_task_groups |
| 24 | total_num_task_groups = n |
| 25 | |
| 26 | |
| 27 | def incre_completed_tasks() -> int: |
| 28 | with num_completed_tasks.get_lock(): |
| 29 | num_completed_tasks.value += 1 |
| 30 | return num_completed_tasks.value |
| 31 | |
| 32 | |
| 33 | def incre_completed_task_groups() -> int: |
| 34 | with num_completed_task_groups.get_lock(): |
| 35 | num_completed_task_groups.value += 1 |
| 36 | return num_completed_task_groups.value |
| 37 | |
| 38 | |
| 39 | def incre_task_return_msg() -> str: |
| 40 | completed = incre_completed_tasks() |
| 41 | completed_groups = num_completed_task_groups.value |
| 42 | return f">>> Completed {completed}/{total_num_tasks} tasks. For groups, completed {completed_groups}/{total_num_task_groups} so far." |
| 43 | |
| 44 | |
| 45 | def incre_task_group_return_msg() -> str: |
| 46 | completed = incre_completed_task_groups() |
| 47 | return f">>>>>> Completed {completed}/{total_num_task_groups} task groups." |