Skip to content

Instantly share code, notes, and snippets.

@peterroelants
Last active August 27, 2022 09:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterroelants/19b4e3a5b89788182417c55082f5563a to your computer and use it in GitHub Desktop.
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
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