dwind 0.3.1__py3-none-any.whl → 0.3.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,60 @@
1
+ import numpy as np
2
+ from joblib import Parallel, delayed
3
+ from threading import Thread
4
+ from rich.progress import Progress, BarColumn, TimeRemainingColumn, TextColumn
5
+ from rich.console import Console
6
+ from rich.live import Live
7
+ import time
8
+
9
+ # Define the number of tasks and create a shared memory numpy array to hold their progress
10
+ num_tasks = 4
11
+ progress_array = np.memmap("progress.mmap2", dtype=np.float32, mode="w+", shape=N)
12
+
13
+ # Define a function that performs a task and updates the progress array
14
+ def perform_task(task_idx, progress_array):
15
+ for i in range(100):
16
+ # Do some work here
17
+ # ...
18
+
19
+ # Update the progress array
20
+ time.sleep(0.1)
21
+ progress_array[task_idx] = i / 100
22
+
23
+ # Update the progress array to 100% on completion
24
+ progress_array[task_idx] = 1
25
+
26
+ # Define a function to continuously update the Rich progress bar
27
+ def update_progress_bar(
28
+ progress_array=progress_array,
29
+ num_tasks=num_tasks,
30
+ ):
31
+ with Progress(
32
+ TextColumn("[bold blue]{task.fields[name]}"),
33
+ BarColumn(),
34
+ TextColumn("[bold green]{task.fields[status]}"),
35
+ TimeRemainingColumn(),
36
+ # console=console,
37
+ ) as progress:
38
+ tasks = [
39
+ progress.add_task(
40
+ description=f"Task {i}",
41
+ name=f"Task {i}",
42
+ status="pending",
43
+ total=100,
44
+ )
45
+ for i in range(num_tasks)
46
+ ]
47
+
48
+ while not all(progress_array == 1):
49
+ for i, task in enumerate(tasks):
50
+ progress.update(task, completed=int(progress_array[i] * 100))
51
+ time.sleep(0.1 * 2 ** abs(*np.random.randn(1)))
52
+
53
+
54
+ # Launch the progress bar update function in a separate thread
55
+ Thread(target=update_progress_bar, args=[progress_array, num_tasks]).start()
56
+
57
+ # Launch the tasks in parallel using joblib and the perform_task function
58
+ Parallel(n_jobs=-2, backend="loky")(
59
+ delayed(perform_task)(i, progress_array) for i in range(num_tasks)
60
+ )