From f4750a3eaf5bdc6067a5f04d99315ac4f9b352af Mon Sep 17 00:00:00 2001 From: benoit-cty <4-benoit-cty@users.noreply.git.leximpact.dev> Date: Fri, 15 Nov 2024 18:53:38 +0100 Subject: [PATCH] Add AnotherInstanceException --- codecarbon/cli/main.py | 24 ++++++++++++++---------- codecarbon/emissions_tracker.py | 12 ++++++++---- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/codecarbon/cli/main.py b/codecarbon/cli/main.py index 5fdaad7e..020e136b 100644 --- a/codecarbon/cli/main.py +++ b/codecarbon/cli/main.py @@ -22,7 +22,7 @@ ) from codecarbon.core.api_client import ApiClient, get_datetime_with_timezone from codecarbon.core.schemas import ExperimentCreate, OrganizationCreate, ProjectCreate -from codecarbon.emissions_tracker import EmissionsTracker +from codecarbon.emissions_tracker import AnotherInstanceException, EmissionsTracker AUTH_CLIENT_ID = os.environ.get( "AUTH_CLIENT_ID", "pkqh9CiOkp4MkPqRqM_k8Xc3mwBRpojS3RayIk1i5Pg" @@ -99,7 +99,7 @@ def show_config(path: Path = Path("./.codecarbon.config")) -> None: fief = Fief(AUTH_SERVER_URL, AUTH_CLIENT_ID) fief_auth = FiefAuth(fief, "./credentials.json") -print("FIEF", AUTH_SERVER_URL, AUTH_CLIENT_ID) +# print("FIEF", AUTH_SERVER_URL, AUTH_CLIENT_ID) def _get_access_token(): @@ -319,14 +319,18 @@ def monitor( if api and experiment_id is None: print("ERROR: No experiment id, call 'codecarbon init' first.", err=True) print("CodeCarbon is going in an infinite loop to monitor this machine.") - with EmissionsTracker( - measure_power_secs=measure_power_secs, - api_call_interval=api_call_interval, - save_to_api=api, - ): - # Infinite loop - while True: - time.sleep(300) + try: + with EmissionsTracker( + measure_power_secs=measure_power_secs, + api_call_interval=api_call_interval, + save_to_api=api, + ): + # Infinite loop + while True: + time.sleep(300) + except AnotherInstanceException: + print("Another instance of CodeCarbon is already running.") + exit(1) def questionary_prompt(prompt, list_options, default): diff --git a/codecarbon/emissions_tracker.py b/codecarbon/emissions_tracker.py index ecaa9fbf..86f9eddf 100644 --- a/codecarbon/emissions_tracker.py +++ b/codecarbon/emissions_tracker.py @@ -64,6 +64,11 @@ _sentinel = object() +# Define an exception to raise when another instance of codecarbon is already running +class AnotherInstanceException(Exception): + pass + + class BaseEmissionsTracker(ABC): """ Primary abstraction with Emissions Tracking functionality. @@ -252,7 +257,7 @@ def __init__( ) # Do not continue if another instance of codecarbon is running self._another_instance_already_running = True - return + raise AnotherInstanceException self._set_from_conf(api_call_interval, "api_call_interval", 8, int) self._set_from_conf(api_endpoint, "api_endpoint", "https://api.codecarbon.io") @@ -515,7 +520,6 @@ def service_shutdown(self, signum, frame): def start(self) -> None: """ Starts tracking the experiment. - Currently, Nvidia GPUs are supported. :return: None """ # if another instance of codecarbon is already running, stop here @@ -526,7 +530,7 @@ def start(self) -> None: logger.warning( "Another instance of codecarbon is already running. Exiting." ) - return + raise AnotherInstanceException if self._start_time is not None: logger.warning("Already started tracking") return @@ -633,7 +637,7 @@ def stop(self) -> Optional[float]: logger.warning( "Another instance of codecarbon is already running. Exiting." ) - return + raise AnotherInstanceException if not self._allow_multiple_runs: # Release the lock self._lock.release()