-
-
Notifications
You must be signed in to change notification settings - Fork 237
Monitor subprocess #431
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
base: master
Are you sure you want to change the base?
Monitor subprocess #431
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| """ | ||
| Implementations of the ``codecarbon monitor`` subcommand. | ||
| """ | ||
| import os | ||
| import subprocess as sp | ||
| import time | ||
| from typing import Sequence | ||
|
|
||
| from codecarbon import EmissionsTracker | ||
|
|
||
|
|
||
| def monitor_infinite_loop(measure_power_secs, api_call_interval, api): | ||
| """ | ||
| Monitor all activity on the system until a user presses CTRL-C. | ||
| """ | ||
| with EmissionsTracker( | ||
| measure_power_secs=measure_power_secs, | ||
| api_call_interval=api_call_interval, | ||
| save_to_api=api, | ||
| tracking_mode="machine", | ||
| ): | ||
| while True: | ||
| time.sleep(300) | ||
|
|
||
|
|
||
| def monitor_subprocess( | ||
| measure_power_secs, api_call_interval, api, cmd: Sequence[str | os.PathLike] | ||
| ) -> int: | ||
| """ | ||
| Run and monitor a subprocess. | ||
| :return: return code of the subprocess. | ||
| """ | ||
|
|
||
| with EmissionsTracker( | ||
| measure_power_secs=measure_power_secs, | ||
| api_call_interval=api_call_interval, | ||
| save_to_api=api, | ||
| tracking_mode="process", | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure if it works with process. It need a test with a process that consume RAM to see the difference with machine because process does not work for GPU and CPU yet.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I confirm launching it like this does not work sadly. For instrumenting a process, it needs to be in the same process it is instrumenting. It will be cool in the future to do something like "instrument process 1234" passing the id. What do you think @jennydaman ? |
||
| ): | ||
| proc = sp.run(cmd) | ||
| return proc.returncode | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note to myself : document the "--no-api" parameter and remove the beta mention for the API.