【发布时间】:2017-12-01 19:52:45
【问题描述】:
有没有办法在笔记本中将 jupyter 笔记本的 CPU 优先级设置得较低?例如。在计算服务器上运行长达一天的参数搜索时,在该笔记本中设置优先级,低于其他人实时处理的笔记本。
【问题讨论】:
标签: python linux environment-variables ipython jupyter-notebook
有没有办法在笔记本中将 jupyter 笔记本的 CPU 优先级设置得较低?例如。在计算服务器上运行长达一天的参数搜索时,在该笔记本中设置优先级,低于其他人实时处理的笔记本。
【问题讨论】:
标签: python linux environment-variables ipython jupyter-notebook
也许为您的python 进程设置优先级就足够了?尽可能的 startet 进程继承优先级。
如果是,以下代码我的帮助:
import psutil
psutil.Process().nice(19)# if on *ux
psutil.Process().nice(psutil.IDLE_PRIORITY_CLASS)# if on win
【讨论】:
我猜你可以修改整个服务器(不知道有什么方法可以修改一个特定的笔记本)。如果其他笔记本在不同的笔记本服务器进程上运行,您可以从子外壳中执行此操作:
---------------------------------------------------------------
!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
或者,如果你想在 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'
}]
根据需要过滤列表以找到所需的列表。
pids = [_['pid'] for _ in servers if meets_condition(_)]
然后调用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
【讨论】:
sh 模块的子 shell,例如 sh.sudo("renice", niceness, pid)。