mlco2 / codecarbon

Track emissions from Compute and recommend ways to reduce their impact on the environment.
https://mlco2.github.io/codecarbon
MIT License
1.17k stars 177 forks source link

Raise an exception when another is running #713

Closed benoit-cty closed 1 week ago

benoit-cty commented 1 week ago

Problem

Currently, when running codecarbon monitor while another instance is active, the CLI displays "Another instance of codecarbon is already running" but continues its infinite loop instead of terminating. This creates a confusing user experience.

Solution

Modify the behavior to raise an exception when detecting another running instance, forcing the program to terminate unless the calling code explicitly handles this case. This makes the error condition more explicit and helps prevent unintended parallel executions of codecarbon monitoring. Please review and provide feedback on this approach.

benoit-cty commented 1 week ago

After a discussion, we prefer to keep this behavior : let the user know in log that CodeCarbon will not work and do not stop his process.

Here is an code sample if a user want to know about CodeCarbon not doing the measure:

   with EmissionsTracker() as tracker:
        # Infinite loop
        while True:
            if (
                hasattr(tracker, "_another_instance_already_running")
                and tracker._another_instance_already_running
            ):
                print("Another instance of CodeCarbon is already running. Exiting.")
                break
            # The compute code goes here
            time.sleep(300)