Skip to content

Commit 54ffc47

Browse files
committed
fix(time): substitui time.time() por time.monotonic() para medições mais precisas
1 parent 877bce3 commit 54ffc47

File tree

6 files changed

+31
-23
lines changed

6 files changed

+31
-23
lines changed

cereja/concurrently/process.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ def _process(self, func, item, *args, **kwargs):
292292
self._failure_data.append(item)
293293

294294
def get_status(self):
295-
return f"{round(self.total_processed / (time.time() - self._started_at), 2)} cpf/s " \
295+
return f"{round(self.total_processed / (time.monotonic() - self._started_at), 2)} cpf/s " \
296296
f"- processing: {self.in_progress_count} " \
297297
f"- success: {self._total_success} " \
298298
f"- fail: {len(self._failure_data)} "
@@ -305,7 +305,7 @@ def process(self, func, data, *args, **kwargs):
305305
self._stopped = False
306306
# inicia thread para atualizar o banco com o resultado da validação.
307307
self._create_process_result_service().start()
308-
self._started_at = time.time()
308+
self._started_at = time.monotonic()
309309

310310
if self._progress is not None:
311311
self._progress.update_max_value(len(data))
@@ -314,12 +314,12 @@ def process(self, func, data, *args, **kwargs):
314314
with ThreadPoolExecutor(max_workers=self._num_workers,
315315
thread_name_prefix="CPF_PROCESS_WORKER") as self._executor:
316316
for item in data:
317-
start_time = time.time()
317+
start_time = time.monotonic()
318318

319319
future = self._executor.submit(self._process, func, item, *args, **kwargs)
320320
self._future_to_data.add(future)
321321

322-
elapsed_time = time.time() - start_time
322+
elapsed_time = time.monotonic() - start_time
323323
# Verifica quanto tempo passou após enviar um dado, caso o tempo for menor que o intervalo
324324
# configurado espera a diferença antes de enviar o próximo lote
325325
if elapsed_time < self.interval_seconds:
@@ -346,7 +346,7 @@ def stop_process(self):
346346
def restart_process(self):
347347
self.stop_process() # espera terminar execução do processo anterior
348348
self._stopped = False
349-
self._started_at = time.time()
349+
self._started_at = time.monotonic()
350350
self._failure_data = []
351351
self._total_success = 0
352352
self._create_process_result_service().start()

cereja/display/_display.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,7 +1068,7 @@ def _get_state(self,
10681068

10691069
@property
10701070
def time_it(self):
1071-
return time.time() - (self._started_time or time.time())
1071+
return time.monotonic() - (self._started_time or time.monotonic())
10721072

10731073
@property
10741074
def total_completed(self):
@@ -1240,7 +1240,7 @@ def start(self):
12401240
self._console.set_prefix(self._name)
12411241
if self._started:
12421242
return
1243-
self._started_time = time.time()
1243+
self._started_time = time.monotonic()
12441244
self._started = True
12451245
try:
12461246
self._th_root.start()
@@ -1306,7 +1306,7 @@ def __call__(self,
13061306
self._console.set_prefix(f"{self.name}({name})")
13071307
if self._with_context and name is None:
13081308
self._console.set_prefix(f"{self.name}(iter-{self._task_count})")
1309-
self._started_time = time.time()
1309+
self._started_time = time.monotonic()
13101310
self._task_count += 1
13111311
return self
13121312

cereja/system/_win32.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,23 +135,23 @@ def t0(self):
135135
def time(self):
136136
if self._t0 is None:
137137
self.start()
138-
self._last_time_check = time.time()
138+
self._last_time_check = time.monotonic()
139139
if self._stopped_on:
140140
return self._stopped_on - self._t0
141-
return time.time() - self._t0
141+
return time.monotonic() - self._t0
142142

143143
@property
144144
def last_check_time(self):
145-
return time.time() - self._last_time_check
145+
return time.monotonic() - self._last_time_check
146146

147147
def start(self):
148148
self._stopped_on = None
149-
self._t0 = time.time()
149+
self._t0 = time.monotonic()
150150
self._last_time_check = self._t0
151151

152152
def stop(self):
153153
if self._started:
154-
self._stopped_on = time.time()
154+
self._stopped_on = time.monotonic()
155155
return
156156
raise Exception("Time counting has not started. Use Time.start method.")
157157

cereja/utils/decorators.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -241,9 +241,9 @@ def time_exec(func: Callable[[Any], Any]) -> Callable:
241241
def wrapper(*args,
242242
**kwargs) -> Any:
243243
from cereja import console
244-
first_time = time.time()
244+
first_time = time.monotonic()
245245
result = func(*args, **kwargs)
246-
console.log(f"[{func.__name__}] performed {time.time() - first_time}")
246+
console.log(f"[{func.__name__}] performed {time.monotonic() - first_time}")
247247
return result
248248

249249
return wrapper
@@ -282,7 +282,7 @@ def wrapper(*args,
282282
def run():
283283
nonlocal last_time
284284
while True:
285-
current_time = time.time()
285+
current_time = time.monotonic()
286286
if current_time - last_time >= interval:
287287
if verbose:
288288
print(f"Running {func.__name__}")
@@ -300,7 +300,7 @@ def run():
300300
def run():
301301
nonlocal last_time
302302
nonlocal last_result
303-
current_time = time.time()
303+
current_time = time.monotonic()
304304
if current_time - last_time >= interval:
305305
if verbose:
306306
print(f"Running {func.__name__}")

cereja/utils/git/runner.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,32 @@
11
import subprocess
22
from .exceptions import GitCommandError
3+
import logging
4+
5+
logger = logging.getLogger(__name__)
36

47

58
class GitCommandRunner:
69
"""Strategy para execução de comandos Git via subprocess."""
710

811
def __init__(self,
9-
repo_path="."):
12+
repo_path=".",
13+
verbose: bool = True):
1014
self.repo_path = repo_path
1115

1216
def run(self,
1317
args: list[str]) -> str:
1418
try:
19+
import sys
1520
result = subprocess.run(
1621
["git"] + args,
1722
cwd=self.repo_path,
18-
capture_output=True,
23+
# capture_output=True,
1924
text=True,
20-
check=True
25+
check=True,
26+
stdout=sys.stdout
2127
)
22-
return result.stdout.strip()
28+
return result.stdout
29+
30+
2331
except subprocess.CalledProcessError as e:
2432
raise GitCommandError(f"Erro ao executar git {' '.join(args)}: {e.stderr.strip()}")

cereja/utils/time.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def start(self):
7373
"""
7474
Start the timer by setting the start time to the current time.
7575
"""
76-
self._start = _time.time()
76+
self._start = time.monotonic()
7777

7878
@property
7979
def started(self) -> bool:
@@ -87,7 +87,7 @@ def elapsed(self):
8787
Returns:
8888
float: The elapsed time in seconds.
8989
"""
90-
return _time.time() - self._start if self.started else 0
90+
return time.monotonic() - self._start if self.started else 0
9191

9292
@property
9393
def remaining(self):

0 commit comments

Comments
 (0)