Skip to content

Instantly share code, notes, and snippets.

@fny
Created August 11, 2022 14:21
Show Gist options
  • Save fny/f7785072aa42a7b4058a63f92bda0013 to your computer and use it in GitHub Desktop.
Save fny/f7785072aa42a7b4058a63f92bda0013 to your computer and use it in GitHub Desktop.
Compute resource usage by Jupyter Notebooks at a given address (RAM and CPU)
import subprocess
import requests
import psutil
def get_notebooks(host, port, token = None):
sessions_url = f'http://{host}:{port}/api/sessions/?token={token}' if token else f'http://{host}:{port}/api/sessions'
response = requests.get(sessions_url).json()
notebooks = [{'kernel_id': notebook['kernel']['id'],
'path': notebook['notebook']['path']} for notebook in response]
for notebook in notebooks:
processes = []
for pid in get_process_ids(notebook['kernel_id']):
processes.append(psutil.Process(pid).as_dict(attrs=['pid', 'name', 'cpu_percent', 'memory_percent', 'create_time']))
pids = [n['pid'] for n in processes]
total_ram = round(sum([n['memory_percent'] for n in processes]), 2)
total_cpu = round(sum([n['cpu_percent'] for n in processes]), 2)
notebook['processes'] = processes
notebook['pids'] = pids
notebook['total_cpu'] = total_cpu
notebook['total_ram'] = total_ram
notebooks = sorted(notebooks, key=lambda x: sum([p['memory_percent'] for p in x['processes']]), reverse=True)
return notebooks
def get_process_ids(name):
child = subprocess.Popen(['pgrep', '-f', name], stdout=subprocess.PIPE, shell=False)
response = child.communicate()[0]
return [int(pid) for pid in response.split()]
def print_notebooks(host, port, token = None):
notebooks = get_notebooks(host, port, token)
for notebook in notebooks:
print(notebook['path'])
print('PIDs: ', notebook['pids'])
print('RAM%: ', f"{notebook['total_ram']}%")
print('CPU%: ', f"{notebook['total_cpu']}%")
print()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment