mlco2 / codecarbon

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

feat: ensure only one instance of codecarbon is run per machine #562

Open inimaz opened 1 month ago

inimaz commented 1 month ago

Imagine I open 2 clis and run codecarbon monitor in each of them. This will result on both of them outputing very similar data to the same output. Now imagine I have multiple instances of codecarbon (e.g. a python script, some cli, some script somewhere that I forgot it is running...). This could become complex rapidly.

Goal of this PR

By default it will allow only one instance per machine to measure at the same time. This is regulated via the parameter allow_multiple_runs.

Default is false.

How to test this

Option 1: using 2 consoles

Open 2 consoles and run hatch run python examples/logging_to_file.py on both of them.

Option 2: multiprocess

I have added an example of exclusive run using multiprocess. Run it via hatch run python examples/logging_to_file_exclusive_run.py Example of output:

~/codecarbon$ hatch run python examples/logging_to_file_exclusive_run.py 
[codecarbon INFO @ 09:43:43] offline tracker init
[codecarbon INFO @ 09:43:43] offline tracker init
[codecarbon INFO @ 09:43:43] offline tracker init
[codecarbon INFO @ 09:43:43] offline tracker init
[codecarbon ERROR @ 09:43:43] Error: Another instance of codecarbon is already running. Turn off the other instance to be able to run this one. Exiting.
[codecarbon ERROR @ 09:43:43] Error: Another instance of codecarbon is already running. Turn off the other instance to be able to run this one. Exiting.
[codecarbon ERROR @ 09:43:43] Error: Another instance of codecarbon is already running. Turn off the other instance to be able to run this one. Exiting.
[codecarbon INFO @ 09:43:43] offline tracker init
[codecarbon ERROR @ 09:43:43] Error: Another instance of codecarbon is already running. Turn off the other instance to be able to run this one. Exiting.
[codecarbon INFO @ 09:43:43] [setup] RAM Tracking...
[codecarbon INFO @ 09:43:43] [setup] GPU Tracking...
...

We see that 5 instances of codecarbon are started and 4 end up in error and exit.