MCPcopy Create free account
hub / github.com/mlco2/codecarbon / monitor

Function monitor

codecarbon/cli/main.py:322–412  ·  view source on GitHub ↗

Monitor your machine's carbon emissions.

(
    ctx: typer.Context,
    measure_power_secs: Annotated[
        int,
        typer.Option(help="Interval between two measures."),
    ] = 10,
    api_call_interval: Annotated[
        int,
        typer.Option(help="Number of measures between API calls."),
    ] = 30,
    api: Annotated[
        bool,
        typer.Option(help="Choose to call Code Carbon API or not"),
    ] = True,
    offline: Annotated[bool, typer.Option(help="Run in offline mode")] = False,
    country_iso_code: Annotated[
        str,
        typer.Option(help="3-letter country ISO code for offline mode"),
    ] = None,
    region: Annotated[
        str,
        typer.Option(help="Region/province for offline mode"),
    ] = None,
)

Source from the content-addressed store, hash-verified

320 context_settings={"allow_extra_args": True, "ignore_unknown_options": True},
321)
322def monitor(
323 ctx: typer.Context,
324 measure_power_secs: Annotated[
325 int,
326 typer.Option(help="Interval between two measures."),
327 ] = 10,
328 api_call_interval: Annotated[
329 int,
330 typer.Option(help="Number of measures between API calls."),
331 ] = 30,
332 api: Annotated[
333 bool,
334 typer.Option(help="Choose to call Code Carbon API or not"),
335 ] = True,
336 offline: Annotated[bool, typer.Option(help="Run in offline mode")] = False,
337 country_iso_code: Annotated[
338 str,
339 typer.Option(help="3-letter country ISO code for offline mode"),
340 ] = None,
341 region: Annotated[
342 str,
343 typer.Option(help="Region/province for offline mode"),
344 ] = None,
345):
346 """Monitor your machine's carbon emissions."""
347
348 # Shared tracker args so monitor and run_and_monitor behave the same
349 tracker_args = {
350 "measure_power_secs": measure_power_secs,
351 "api_call_interval": api_call_interval,
352 }
353 # Set up the tracker arguments based on mode (offline vs online) and validate required args for each mode
354 if offline:
355 if not country_iso_code:
356 print(
357 "ERROR: Country ISO code is required for offline mode. Add it to your configuration or provide it via the command line: `--country-iso-code FRA`",
358 file=sys.stderr,
359 )
360 raise typer.Exit(1)
361
362 tracker_args = {
363 **tracker_args,
364 "country_iso_code": country_iso_code,
365 "region": region,
366 }
367 else:
368 experiment_id = get_existing_exp_id()
369 if api and experiment_id is None:
370 print(
371 "ERROR: No experiment id. Set CODECARBON_EXPERIMENT_ID, call 'codecarbon config' first, or run in offline mode with `--offline --country-iso-code FRA`.",
372 file=sys.stderr,
373 )
374 raise typer.Exit(1)
375
376 tracker_args = {**tracker_args, "save_to_api": api}
377
378 # If extra args are provided (e.g. `codecarbon monitor -- my_script.py`), delegate to `run_and_monitor`
379 if getattr(ctx, "args", None):

Callers

nothing calls this directly

Calls 6

get_existing_exp_idFunction · 0.90
run_and_monitorFunction · 0.90
EmissionsTrackerClass · 0.90
startMethod · 0.45
stopMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…