Created
November 5, 2019 07:11
-
-
Save Deadlyelder/6491c690905003cd0b63f58dda0241e2 to your computer and use it in GitHub Desktop.
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
import subprocess | |
import re | |
from tornado import gen, web | |
from notebook.base.handlers import APIHandler | |
import psutil | |
KERNEL_REGEX = re.compile(r'.+kernel-(.+)\.json') | |
PID_REGEX = re.compile(r'(\d+).+') | |
class MemHand(APIHandler): | |
''' | |
get memory of each kernel | |
''' | |
@web.authenticated | |
@gen.coroutine | |
def get(self): | |
ps = subprocess.Popen(('ps', 'ax'), stdout=subprocess.PIPE) | |
output = ps.communicate()[0] | |
result={} | |
for line in output.split('\n'): | |
if 'kernel-' in line: | |
kernel_id = re.sub(KERNEL_REGEX, r'\1', line) | |
pid = int(re.sub(PID_REGEX, r'\1', line)) | |
process = psutil.Process(pid) | |
mem = int(process.memory_info()[0]) | |
result[kernel_id]={'pid':pid,'memory': mem} | |
self.finish(result) | |
default_handlers = [ | |
(r"/api/kernel/memory", MemHand) | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment