Add common class to access to the CPU

This commit is contained in:
Nicolargo 2015-02-03 16:40:07 +01:00
parent ac2c1ba9e0
commit c66a7a8fd2

View File

@ -0,0 +1,48 @@
# -*- coding: utf-8 -*-
#
# This file is part of Glances.
#
# Copyright (C) 2015 Nicolargo <nicolas@nicolargo.com>
#
# Glances is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Glances is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""CPU percent stats shared between CPU and Quicklook plugins."""
import psutil
from glances.core.glances_timer import Timer
class CpuPercent(object):
"""Get and strore the CPU percent"""
def __init__(self, cached_time=1):
self.cpu_percent = 0
# cached_time is the minimum time interval between stats updates
# since last update is passed (will retrieve old cached info instead)
self.timer = Timer(0)
self.cached_time = cached_time
def get(self):
"""Update and/or return the CPU using the PSUtil lib"""
# Never update more than 1 time per cached_time
if self.timer.finished():
self.cpu_percent = psutil.cpu_percent(interval=0.0)
self.timer = Timer(self.cached_time)
return self.cpu_percent
# CpuPercent instance shared between plugins
cpu_percent = CpuPercent()