在 jupyter/ipython notebook 中设置优先级/友好度

Tim*_*Tim 5 python linux environment-variables ipython jupyter-notebook

有什么方法可以在笔记本内将 jupyter 笔记本的 CPU 优先级设置得更低?例如,当在计算服务器上运行长达数天的参数搜索时,在该笔记本中设置优先级在其他人正在实时处理的笔记本之后。

Pau*_*ine 1

我想您可以重新启动整个服务器(不知道有任何方法重新启动特定笔记本)。如果其他笔记本在不同的笔记本服务器进程上运行,您可以从子 shell 中执行此操作:

---------------------------------------------------------------
  !for pid in `pgrep -f jupyter`; do { renice -20 $pid; }; done
---------------------------------------------------------------

9721 (process ID) old priority 19, new priority -20
12449 (process ID) old priority 19, new priority -20
25502 (process ID) old priority 19, new priority -20
Run Code Online (Sandbox Code Playgroud)

或者,如果您想在 Python 中执行此操作,请首先获取正在运行的服务器列表:

>>> from notebook import notebookapp
>>> servers = list(notebookapp.list_running_servers())
>>> servers
[{'url': 'http://localhost:8888/', 
  'base_url': '/', 
  'token': '5ea29b3...7e1fba5331ae', 
  'secure': False, 
  'pid': 9721, 
  'hostname': 'localhost', 
  'password': False, 
  'port': 8888, 
  'notebook_dir': '/home/paulos/work'
}]
Run Code Online (Sandbox Code Playgroud)

如果需要,可以过滤列表中您想要的内容。

pids = [_['pid'] for _ in servers if meets_condition(_)]
Run Code Online (Sandbox Code Playgroud)

然后调用setpriority:

>>> from ctypes import cdll
>>> libc = cdll.LoadLibrary("libc.so.6")
>>> pids = [_['pid'] for _ in servers]
>>> for pid in pids:
        print("old priority for PID", pid, "is", libc.getpriority(0, pid))
        libc.setpriority(0, pid, 20)
        print("new priority for PID", pid, "is", libc.getpriority(0, pid))

old priority for PID 9721 is 0
new priority for PID 9721 is 19
Run Code Online (Sandbox Code Playgroud)