|
1
|
|
|
import itertools |
|
|
|
|
|
|
2
|
|
|
import logging |
|
3
|
|
|
import multiprocessing |
|
4
|
|
|
import time |
|
5
|
|
|
import warnings |
|
|
|
|
|
|
6
|
|
|
from datetime import datetime |
|
7
|
|
|
from typing import ( |
|
8
|
|
|
Any, |
|
9
|
|
|
Callable, |
|
10
|
|
|
Collection, |
|
11
|
|
|
Generator, |
|
12
|
|
|
Iterable, |
|
13
|
|
|
Iterator, |
|
14
|
|
|
Optional, |
|
15
|
|
|
TypeVar, |
|
16
|
|
|
Union, |
|
17
|
|
|
) |
|
18
|
|
|
|
|
19
|
|
|
from pocketutils.tools.base_tools import BaseTools |
|
20
|
|
|
from pocketutils.tools.unit_tools import UnitTools |
|
21
|
|
|
|
|
22
|
|
|
logger = logging.getLogger("pocketutils") |
|
23
|
|
|
T = TypeVar("T") |
|
|
|
|
|
|
24
|
|
|
K = TypeVar("K", covariant=True) |
|
|
|
|
|
|
25
|
|
|
V = TypeVar("V", contravariant=True) |
|
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
class LoopTools(BaseTools): |
|
|
|
|
|
|
29
|
|
|
@classmethod |
|
30
|
|
|
def loop( |
|
31
|
|
|
cls, |
|
|
|
|
|
|
32
|
|
|
things: Iterable[T], |
|
|
|
|
|
|
33
|
|
|
*, |
|
|
|
|
|
|
34
|
|
|
log: Union[None, str, Callable[[str], Any]] = None, |
|
|
|
|
|
|
35
|
|
|
every_i: int = 10, |
|
|
|
|
|
|
36
|
|
|
n_total: Optional[int] = None, |
|
|
|
|
|
|
37
|
|
|
) -> Generator[T, None, None]: |
|
38
|
|
|
""" |
|
39
|
|
|
Loops over elements while logging time taken, % processed, etc. |
|
40
|
|
|
|
|
41
|
|
|
Args: |
|
42
|
|
|
things: Items to process; if sized or ``n_total`` is passed, |
|
43
|
|
|
will log estimates and % complete |
|
44
|
|
|
log: Write by calling this function. |
|
45
|
|
|
The returned value is ignored. |
|
46
|
|
|
if ``str``, gets a function from python logging |
|
47
|
|
|
(via :attr:`pocketutils.tools.loop_tools.logger`); |
|
48
|
|
|
see :meth:`get_log_function`. |
|
49
|
|
|
If ``None``, does not log. |
|
50
|
|
|
every_i: Log after every ``every_i`` items processed |
|
51
|
|
|
n_total: Provide if ``things`` is not sized yet the total number |
|
52
|
|
|
of items is known |
|
53
|
|
|
""" |
|
54
|
|
|
log = cls.get_log_function(log) |
|
55
|
|
|
if hasattr(things, "__len__") or n_total is not None: |
|
56
|
|
|
# noinspection PyTypeChecker |
|
57
|
|
|
yield from cls._loop_timing(things, log, every_i, n_total) |
|
58
|
|
|
else: |
|
59
|
|
|
yield from cls._loop_logging(things, log, every_i) |
|
60
|
|
|
|
|
61
|
|
|
@classmethod |
|
62
|
|
|
def parallel( |
|
63
|
|
|
cls, |
|
|
|
|
|
|
64
|
|
|
items: Collection[K], |
|
|
|
|
|
|
65
|
|
|
function: Callable[[K], V], |
|
|
|
|
|
|
66
|
|
|
*, |
|
|
|
|
|
|
67
|
|
|
to=print, |
|
|
|
|
|
|
68
|
|
|
n_cores: int = 2, |
|
|
|
|
|
|
69
|
|
|
poll_sec: float = 0.4, |
|
|
|
|
|
|
70
|
|
|
) -> None: |
|
71
|
|
|
""" |
|
72
|
|
|
Process items with multiprocessing and a rotating cursor with % complete. |
|
73
|
|
|
|
|
74
|
|
|
Args: |
|
75
|
|
|
items: Items to process; must have ``__len__`` defined |
|
76
|
|
|
function: Called per item |
|
77
|
|
|
to: Write (log) to this function; ``end="\r"`` is passed for the cursor |
|
78
|
|
|
n_cores: The number pool cores |
|
79
|
|
|
poll_sec: Check for new every ``poll_sec`` seconds |
|
80
|
|
|
""" |
|
81
|
|
|
t0 = time.monotonic() |
|
|
|
|
|
|
82
|
|
|
if to is not None: |
|
83
|
|
|
to(f"Using {n_cores} cores...") |
|
84
|
|
|
with multiprocessing.Pool(n_cores) as pool: |
|
85
|
|
|
queue = multiprocessing.Manager().Queue() |
|
86
|
|
|
result = pool.starmap_async(function, items) |
|
87
|
|
|
cycler = itertools.cycle(r"\|/―") |
|
88
|
|
|
while not result.ready(): |
|
89
|
|
|
percent = queue.qsize() / len(items) |
|
90
|
|
|
if to is not None: |
|
91
|
|
|
to(f"% complete: {percent:.0%} {next(cycler)}", end="\r") |
|
92
|
|
|
time.sleep(poll_sec) |
|
93
|
|
|
got = result.get() |
|
94
|
|
|
delta = UnitTools.delta_time_to_str(time.monotonic() - t0) |
|
95
|
|
|
if to is not None: |
|
96
|
|
|
to(f"\nProcessed {len(got)} items in {delta}") |
|
97
|
|
|
|
|
98
|
|
|
@classmethod |
|
99
|
|
|
def _loop_logging( |
|
100
|
|
|
cls, |
|
|
|
|
|
|
101
|
|
|
things: Iterable[T], |
|
|
|
|
|
|
102
|
|
|
log: Union[None, str, Callable[[str], None]] = None, |
|
|
|
|
|
|
103
|
|
|
every_i: int = 10, |
|
|
|
|
|
|
104
|
|
|
) -> Iterator[T]: |
|
105
|
|
|
log = cls.get_log_function(log) |
|
106
|
|
|
initial_start_time = time.monotonic() |
|
107
|
|
|
now = datetime.now().strftime("%Y-%m-%d %H:%M:%S") |
|
108
|
|
|
log(f"Started processing at {now}.") |
|
|
|
|
|
|
109
|
|
|
i = 0 |
|
110
|
|
|
for i, thing in enumerate(things): |
|
111
|
|
|
t0 = time.monotonic() |
|
|
|
|
|
|
112
|
|
|
yield thing |
|
113
|
|
|
t1 = time.monotonic() |
|
|
|
|
|
|
114
|
|
|
if i % every_i == 0 and i > 0: |
|
115
|
|
|
elapsed_s = UnitTools.delta_time_to_str(t1 - t0) |
|
116
|
|
|
log(f"Processed next {every_i} in {elapsed_s}") |
|
|
|
|
|
|
117
|
|
|
now = datetime.now().strftime("%Y-%m-%d %H:%M:%S") |
|
118
|
|
|
delta = UnitTools.delta_time_to_str(time.monotonic() - initial_start_time) |
|
119
|
|
|
log(f"Processed {i}/{i} in {delta}. Done at {now}.") |
|
|
|
|
|
|
120
|
|
|
|
|
121
|
|
|
@classmethod |
|
122
|
|
|
def _loop_timing( |
|
123
|
|
|
cls, |
|
|
|
|
|
|
124
|
|
|
things: Collection[Any], |
|
|
|
|
|
|
125
|
|
|
log: Union[None, str, Callable[[str], None]] = None, |
|
|
|
|
|
|
126
|
|
|
every_i: int = 10, |
|
|
|
|
|
|
127
|
|
|
n_total: Optional[int] = None, |
|
|
|
|
|
|
128
|
|
|
): |
|
129
|
|
|
log = cls.get_log_function(log) |
|
130
|
|
|
n_total = len(things) if n_total is None else n_total |
|
131
|
|
|
now = datetime.now().strftime("%Y-%m-%d %H:%M:%S") |
|
132
|
|
|
log(f"Started processing {n_total} items at {now}.") |
|
|
|
|
|
|
133
|
|
|
t0 = time.monotonic() |
|
|
|
|
|
|
134
|
|
|
initial_start_time = time.monotonic() |
|
135
|
|
|
for i, thing in enumerate(things): |
|
136
|
|
|
yield thing |
|
137
|
|
|
t1 = time.monotonic() |
|
|
|
|
|
|
138
|
|
|
if i % every_i == 0 and i < n_total - 1: |
|
139
|
|
|
estimate = (t1 - initial_start_time) / (i + 1) * (n_total - i - 1) |
|
140
|
|
|
elapsed_s = UnitTools.delta_time_to_str(t1 - t0) |
|
141
|
|
|
estimate_s = UnitTools.delta_time_to_str(estimate) |
|
142
|
|
|
log(f"Processed {i+1}/{n_total} in {elapsed_s}. Estimated {estimate_s} left.") |
|
|
|
|
|
|
143
|
|
|
now = datetime.now().strftime("%Y-%m-%d %H:%M:%S") |
|
144
|
|
|
delta = UnitTools.delta_time_to_str(time.monotonic() - initial_start_time) |
|
145
|
|
|
log(f"Processed {n_total}/{n_total} in {delta}. Done at {now}.") |
|
|
|
|
|
|
146
|
|
|
|
|
147
|
|
|
|
|
148
|
|
|
__all__ = ["LoopTools"] |
|
149
|
|
|
|