Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise an exception when another is running #713

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions codecarbon/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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():
Expand Down Expand Up @@ -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):
Expand Down
12 changes: 8 additions & 4 deletions codecarbon/emissions_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand Down
Loading