我正在尝试将某些流程优先于其他流程。这是我正在使用的主要脚本,它模拟了一个 CPU 密集型进程:
simple_app.py
import os
from multiprocessing import Pool, cpu_count
def f(x):
while True:
x*x
if __name__ == '__main__':
cpu = cpu_count()
pid = os.getpid()
print('-' * 20)
print('pid: {}'.format(pid))
print('Utilizing {} cores'.format(cpu))
print('Current niceness: {}'.format(os.nice(0)))
print('-' * 20)
pool = Pool(cpu)
pool.map(f, range(cpu))
我的下一步是生成大量(具体而言,在本例中为 9 个)运行此代码的进程:
simple_runner.sh
# Start with lowest priority
nice -19 python3 simple_app.py &
# Much higher priority
nice -0 python3 simple_app.py &
# Lower priority spawned
nice -10 python3 simple_app.py &
# Higher priority again
nice -7 python3 simple_app.py &
# Highest priority yet
nice -1 python3 simple_app.py &
# Highest priority yet
nice -0 python3 simple_app.py &
# Highest priority yet
nice -0 python3 simple_app.py &
# Highest priority yet
nice -0 python3 simple_app.py &
# Highest priority yet
nice -0 python3 simple_app.py
然后我监视每个进程,报告子 CPU 利用率,这里:
process_reporting_server.py
import os
import time
import argparse
import pprint
from multiprocessing import Pool, cpu_count
import psutil
def most_recent_process_info(pid, interval=0.5):
while True:
proc = psutil.Process(pid)
children_cpu_percent = [child.cpu_percent(interval) for child in proc.children()]
children_cpu_percent_mean = sum(children_cpu_percent) / len(children_cpu_percent) if children_cpu_percent else -1.
print('Time: {}, PID: {}, niceness: {}, average child CPU percent: {:.2f}'.format(
time.ctime(),
pid,
proc.nice(),
children_cpu_percent_mean)
)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-p', '--pids', type=str, help='Whitespace-delimited string containing PIDs', dest='pids')
parser.add_argument('-s', '--seconds', type=int, help='Seconds to sleep', default=10, dest='seconds')
args = parser.parse_args()
pids = list(map(int, args.pids.split()))
pool = Pool(len(pids))
pool.map(most_recent_process_info, pids)
我想看看被赋予较低 niceness 值的进程是否实际上被优先考虑。所以这就是我所做的:
运行simple_app_runner.sh
:
$ ./simple_app_runner.sh
--------------------
pid: 45036
Utilizing 8 cores
Current niceness: 0
--------------------
--------------------
pid: 45030
Utilizing 8 cores
Current niceness: 19
--------------------
--------------------
pid: 45034
Utilizing 8 cores
Current niceness: 1
--------------------
--------------------
pid: 45032
Utilizing 8 cores
Current niceness: 10
--------------------
--------------------
pid: 45033
Utilizing 8 cores
Current niceness: 7
--------------------
--------------------
pid: 45037
Utilizing 8 cores
Current niceness: 0
--------------------
--------------------
pid: 45038
Utilizing 8 cores
Current niceness: 0
--------------------
--------------------
pid: 45031
Utilizing 8 cores
Current niceness: 0
--------------------
--------------------
pid: 45035
Utilizing 8 cores
Current niceness: 0
--------------------
然后,这是报告:
$ python3 process_reporting_server.py -p '45036 45030 45034 45032 45033 45037 45038 45031 45035'
稍微清理一下并使用 pandas 进行分析,我们看到在五分钟的时间间隔内,指定的好坏似乎并不重要:
>>> df.groupby('nice')['mean_child_cpu'].max()
nice
0.0 10.50
1.0 9.75
7.0 8.28
10.0 8.50
19.0 21.97
我在这里完全错过了什么吗?为什么我指定的 niceness 似乎不会影响 CPU 资源的优先级?