Skip to content

Commit 11374f4

Browse files
perf: defer tracker initialization and slim import path (1/4) (#1251)
* perf: defer tracker initialization and slim import path Lazy-load reference data, output handlers, hardware probing, and the emissions engine so tracker construction stays fast when work is deferred. Co-authored-by: Cursor <cursoragent@cursor.com> * test: initialize hardware before cpu_load detection assertion Deferred hardware setup in PR 1 requires _ensure_hardware_ready() in tests that inspect tracker._hardware before start(). Co-authored-by: Cursor <cursoragent@cursor.com> * fix: restore pandas DataFrame type hints in input.py Use TYPE_CHECKING import for pd so lazy loading stays fast while keeping return type annotations on get_cloud_emissions_data and get_cpu_power_data. Co-authored-by: Cursor <cursoragent@cursor.com> --------- Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 150cac8 commit 11374f4

13 files changed

Lines changed: 220 additions & 107 deletions

codecarbon/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
OfflineEmissionsTracker,
99
track_emissions,
1010
)
11-
from .output import OutputMethod
11+
from .output_methods.base_output import OutputMethod
1212

1313
__all__ = [
1414
"EmissionsTracker",

codecarbon/core/api_client.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import json
1111
from datetime import timedelta, tzinfo
1212

13-
import arrow
1413
import requests
1514

1615
from codecarbon.core.schemas import (
@@ -22,12 +21,11 @@
2221
)
2322
from codecarbon.external.logger import logger
2423

25-
# from codecarbon.output import EmissionsData
26-
2724

2825
def get_datetime_with_timezone():
29-
timestamp = str(arrow.now().isoformat())
30-
return timestamp
26+
import arrow
27+
28+
return str(arrow.now().isoformat())
3129

3230

3331
class ApiClient: # (AsyncClient)

codecarbon/core/util.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
from pathlib import Path
99
from typing import Optional, Union
1010

11-
import cpuinfo
1211
import psutil
1312

1413
from codecarbon.external.logger import logger
@@ -76,6 +75,8 @@ def backup(file_path: Union[str, Path], ext: Optional[str] = ".bak") -> None:
7675

7776
@lru_cache(maxsize=1)
7877
def detect_cpu_model() -> Optional[str]:
78+
import cpuinfo
79+
7980
cpu_info = cpuinfo.get_cpu_info()
8081
if cpu_info:
8182
cpu_model_detected = cpu_info.get("brand_raw", "")

0 commit comments

Comments
 (0)