MCPcopy Create free account
hub / github.com/aaPanel/BaoTa / get_cpu_precent

Method get_cpu_precent

class/projectModel/pythonModel.py:3400–3421  ·  view source on GitHub ↗

@name 获取进程cpu使用率 @author hwliang<2021-08-09> @param p: Process<进程对像> @return dict

(self, p)

Source from the content-addressed store, hash-verified

3398 return process_info
3399
3400 def get_cpu_precent(self, p):
3401 ''&#x27;
3402 @name 获取进程cpu使用率
3403 @author hwliang<2021-08-09>
3404 @param p: Process<进程对像>
3405 @return dict
3406 ''&#x27;
3407 skey = "cpu_pre_{}".format(p.pid)
3408 old_cpu_times = cache.get(skey)
3409
3410 process_cpu_time = self.get_process_cpu_time(p.cpu_times())
3411 if not old_cpu_times:
3412 cache.set(skey, [process_cpu_time, time.time()], 3600)
3413 old_cpu_times = cache.get(skey)
3414 process_cpu_time = self.get_process_cpu_time(p.cpu_times())
3415
3416 old_process_cpu_time = old_cpu_times[0]
3417 old_time = old_cpu_times[1]
3418 new_time = time.time()
3419 cache.set(skey, [process_cpu_time, new_time], 3600)
3420 percent = round(100.00 * (process_cpu_time - old_process_cpu_time) / (new_time - old_time) / psutil.cpu_count(), 2)
3421 return percent
3422
3423 @staticmethod
3424 def get_process_cpu_time(cpu_times):

Callers 1

Calls 5

get_process_cpu_timeMethod · 0.95
timeMethod · 0.80
formatMethod · 0.45
getMethod · 0.45
setMethod · 0.45

Tested by

no test coverage detected