-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_multi_processes.py
More file actions
45 lines (39 loc) · 1.24 KB
/
Copy pathtest_multi_processes.py
File metadata and controls
45 lines (39 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from __future__ import annotations
import multiprocessing
import time
from concurrent.futures import ProcessPoolExecutor
from file_progress import (
MultiWorkerFileProgress,
get_sub_progress_process,
)
def _run_worker(args: tuple[int, int]):
worker_id, steps = args
sub_progress = get_sub_progress_process(worker_id)
for step in range(1, steps + 1):
sub_progress.update(
f"process worker {worker_id}",
step,
steps,
extra=f"task={step} phase=sleep",
)
time.sleep(1)
sub_progress.increment_total(1)
return worker_id
if __name__ == "__main__":
worker_steps = [3, 4, 5]
ctx = multiprocessing.get_context("spawn")
with MultiWorkerFileProgress(
desc="process pool demo",
total=sum(worker_steps),
interval_seconds=0.0,
cleanup_on_success=False,
verbose=2,
) as progress:
initializer, initargs = progress.process_initializer(ctx)
with ProcessPoolExecutor(
max_workers=len(worker_steps),
mp_context=ctx,
initializer=initializer,
initargs=initargs,
) as executor:
list(executor.map(_run_worker, enumerate(worker_steps, start=1)))