Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /mlco2_codecarbon /codecarbon /cli /monitor.py
| """CodeCarbon CLI - Monitor Command""" | |
| import os | |
| import subprocess | |
| import sys | |
| from typing import Optional | |
| import typer | |
| from rich import print | |
| from typing_extensions import Annotated | |
| def run_and_monitor( | |
| ctx: typer.Context, | |
| log_level: Annotated[ | |
| Optional[str], | |
| typer.Option(help="Log level (critical, error, warning, info, debug)"), | |
| ] = None, | |
| offline: bool = False, | |
| **tracker_args, | |
| ): | |
| """ | |
| Run a command and track its carbon emissions. | |
| This command wraps any executable and measures the process's total power | |
| consumption during its execution. When the command completes, a summary | |
| report is displayed and emissions data is saved to a CSV file. | |
| Note: This tracks process-level emissions (only the specific command), not the | |
| entire machine. For machine-level tracking, use the `monitor` command. | |
| Examples: | |
| Do not use quotes around the command. Use -- to separate CodeCarbon args. | |
| # Run any shell command: | |
| codecarbon monitor -- ./benchmark.sh | |
| # Commands with arguments (use single quotes for special chars): | |
| codecarbon monitor -- python -c 'print("Hello World!")' | |
| # Pipe the command output: | |
| codecarbon monitor -- npm run test > output.txt | |
| # Display the CodeCarbon detailed logs: | |
| codecarbon monitor --log-level debug -- python --version | |
| The emissions data is appended to emissions.csv (default) in the current | |
| directory. The file path is shown in the final report. | |
| """ | |
| # Suppress all CodeCarbon logs during execution | |
| from codecarbon.emissions_tracker import EmissionsTracker, OfflineEmissionsTracker | |
| from codecarbon.external.logger import set_logger_level | |
| # `log_level` is None when nothing set it: leave it to the tracker, which | |
| # resolves it from the configuration file and the environment. | |
| if log_level is not None: | |
| set_logger_level(log_level) | |
| tracker_args["log_level"] = log_level | |
| # Get the command from remaining args (strip nested subcommand / `--` leftovers) | |
| command = list(getattr(ctx, "args", None) or []) | |
| while command and command[0] in ("monitor", "--"): | |
| command.pop(0) | |
| if not command: | |
| print( | |
| "ERROR: No command provided. Use: codecarbon monitor -- <command>", | |
| file=sys.stderr, | |
| ) | |
| raise typer.Exit(1) | |
| tracker_cls = OfflineEmissionsTracker if offline else EmissionsTracker | |
| tracker = tracker_cls( | |
| save_to_logger=False, | |
| tracking_mode="process", | |
| **tracker_args, | |
| ) | |
| print("🌱 CodeCarbon: Starting emissions tracking...") | |
| print(f" Command: {' '.join(command)}") | |
| print() | |
| tracker.start() | |
| process = None | |
| try: | |
| # Run the command, streaming output to console | |
| # Let the child inherit the parent's std streams so Click's | |
| # `CliRunner` can capture output (don't pass StringIO objects). | |
| process = subprocess.Popen(command, text=True) | |
| # Wait for completion | |
| exit_code = process.wait() | |
| except FileNotFoundError: | |
| print(f"❌ Error: Command not found: {command[0]}", file=sys.stderr) | |
| exit_code = 127 | |
| except KeyboardInterrupt: | |
| print("\n⚠️ Interrupted by user", file=sys.stderr) | |
| if process is not None: | |
| process.terminate() | |
| try: | |
| process.wait(timeout=5) | |
| except subprocess.TimeoutExpired: | |
| process.kill() | |
| exit_code = 130 | |
| except Exception as e: | |
| print(f"❌ Error running command: {e}", file=sys.stderr) | |
| exit_code = 1 | |
| finally: | |
| emissions = tracker.stop() | |
| print() | |
| print("=" * 60) | |
| print("🌱 CodeCarbon Emissions Report") | |
| print("=" * 60) | |
| print(f" Command: {' '.join(command)}") | |
| if emissions is not None: | |
| print(f" Emissions: {emissions * 1000:.4f} g CO2eq") | |
| else: | |
| print(" Emissions: N/A") | |
| # Show where the data was saved | |
| if hasattr(tracker, "_conf") and "output_file" in tracker._conf: | |
| output_path = tracker._conf["output_file"] | |
| # Make it absolute if it's relative | |
| if not os.path.isabs(output_path): | |
| output_path = os.path.abspath(output_path) | |
| print(f" Saved to: {output_path}") | |
| print(" ⚠️ Note: Tracked the command process and its children") | |
| print("=" * 60) | |
| raise typer.Exit(exit_code) | |
Xet Storage Details
- Size:
- 4.51 kB
- Xet hash:
- 5865c63a052d8db881591d3bc3a8c08cb6338dd73160b39e8c092af806bc18f9
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.