我似乎已经为自己的问题找到了可行的解决方案:
import os import pwd import psutil import re import string import json import urllib2 import pandas as pd UID = 1 EUID = 2 regex = re.compile(r'.+kernel-(.+)\.json') pids = [pid for pid in os.listdir('/proc') if pid.isdigit()] # memory info from psutil.Process df_mem = [] for pid in pids: try: ret = open(os.path.join('/proc', pid, 'cmdline'), 'rb').read() except IOError: # proc has already terminated continue # jupyter notebook processes if len(ret) > 0 and 'share/jupyter/runtime' in ret: # kernel kernel_ID = re.sub(regex, r'\1', ret) kernel_ID = filter(lambda x: x in string.printable, kernel_ID) # memory process = psutil.Process(int(pid)) mem = process.memory_info()[0] / float(1e9) # user name for pid for ln in open('/proc/{}/status'.format(int(pid))): if ln.startswith('Uid:'): uid = int(ln.split()[UID]) uname = pwd.getpwuid(uid).pw_name # user, pid, memory, kernel_ID df_mem.append([uname, pid, mem, kernel_ID]) df_mem = pd.DataFrame(df_mem) df_mem.columns = ['user', 'pid', 'memory_GB', 'kernel_ID'] # notebook info from assessing ports df_nb = [] for port in xrange(5000,30000): sessions = None try: url = 'http://127.0.0.1:{}/api/sessions'.format(port) sessions = json.load(urllib2.urlopen(url)) except urllib2.URLError: sessions = None if sessions: for sess in sessions: kernel_ID = str(sess['kernel']['id']) notebook_path = sess['notebook']['path'] df_nb.append([port, kernel_ID, notebook_path]) df_nb = pd.DataFrame(df_nb) df_nb.columns = ['port', 'kernel_ID', 'notebook_path'] # joining tables df = pd.merge(df_nb, df_mem, on=['kernel_ID'], how='inner') df.sort(['memory_GB'], ascending=False)