Last active
August 27, 2022 09:29
-
-
Save peterroelants/19b4e3a5b89788182417c55082f5563a to your computer and use it in GitHub Desktop.
Run nvidia-smi in notebook cell that keeps updating while other cells do their work
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from datetime import timedelta | |
import multiprocessing | |
import subprocess | |
import time | |
from IPython.display import clear_output | |
def nvidia_smi_call(): | |
""" | |
Call `nvidia-smi` in the background and refresh the cell output with the stdout every second | |
The cell where this is run in will not block and other cells can be run after it while it keeps updating. | |
""" | |
start_time = time.time() | |
count = 0 | |
while True: | |
clear_output(wait=True) | |
result = subprocess.run(["nvidia-smi"], stdout=subprocess.PIPE) | |
time_delta = timedelta(seconds=time.time() - start_time) | |
print( | |
result.stdout.decode("utf-8") + "\n" | |
+ f"Run for: {time_delta!s}s ({count} updates)" | |
) | |
time.sleep(1) | |
count += 1 | |
nvidia_smi_proc = multiprocessing.Process(target=nvidia_smi_call) | |
nvidia_smi_proc.start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment