當前位置: 首頁>>代碼示例>>Python>>正文


Python psutil.cpu_times_percent方法代碼示例

本文整理匯總了Python中psutil.cpu_times_percent方法的典型用法代碼示例。如果您正苦於以下問題:Python psutil.cpu_times_percent方法的具體用法?Python psutil.cpu_times_percent怎麽用?Python psutil.cpu_times_percent使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在psutil的用法示例。


在下文中一共展示了psutil.cpu_times_percent方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: cpu_times_percent

# 需要導入模塊: import psutil [as 別名]
# 或者: from psutil import cpu_times_percent [as 別名]
def cpu_times_percent():
    c = statsd.StatsClient(STATSD_HOST, 8125, prefix=PREFIX + 'system.cpu')
    while True:
        value = psutil.cpu_percent(interval=1)
        c.gauge('system_wide.percent', value)

        cpu_t_percent = psutil.cpu_times_percent(interval=1)
        c.gauge('system_wide.times_percent.user', cpu_t_percent.user)
        c.gauge('system_wide.times_percent.nice', cpu_t_percent.nice)
        c.gauge('system_wide.times_percent.system', cpu_t_percent.system)
        c.gauge('system_wide.times_percent.idle', cpu_t_percent.idle)
        c.gauge('system_wide.times_percent.iowait', cpu_t_percent.iowait)
        c.gauge('system_wide.times_percent.irq', cpu_t_percent.irq)
        c.gauge('system_wide.times_percent.softirq', cpu_t_percent.softirq)
        c.gauge('system_wide.times_percent.steal', cpu_t_percent.steal)
        c.gauge('system_wide.times_percent.guest', cpu_t_percent.guest)
        c.gauge('system_wide.times_percent.guest_nice', cpu_t_percent.guest_nice)
        time.sleep(GRANULARITY) 
開發者ID:thenetcircle,項目名稱:dino,代碼行數:20,代碼來源:statsd-agent.py

示例2: OSinfo

# 需要導入模塊: import psutil [as 別名]
# 或者: from psutil import cpu_times_percent [as 別名]
def OSinfo():
    '''操作係統基本信息查看'''
    core_number = psutil.cpu_count()
    cpu_number = psutil.cpu_count(logical=True)
    cpu_usage_precent = psutil.cpu_times_percent()
    mem_info = psutil.virtual_memory()
    result = {
        "memtotal": mem_info[0],
        "memavail": mem_info[1],
        "memprecn": mem_info[2],
        "memusage": mem_info[3],
        "memfreed": mem_info[4],
    }
    print '''
        內核版本 : %s
        CORE數量 : %s
        CPU數量 : %s
        CPU使用率 : %s
        內存總量  : %s
        內存使用率 : %s
    '''%(str(platform.platform()),str(core_number),str(cpu_number),str(cpu_usage_precent),str(mem_info[0]),str(mem_info[2])) 
開發者ID:cisp,項目名稱:LinuxEmergency,代碼行數:23,代碼來源:emergency.py

示例3: crawl

# 需要導入模塊: import psutil [as 別名]
# 或者: from psutil import cpu_times_percent [as 別名]
def crawl(self, **kwargs):
        logger.debug('Crawling %s' % (self.get_feature()))

        for (idx, cpu) in enumerate(psutil.cpu_times_percent(percpu=True)):
            feature_attributes = CpuFeature(
                cpu.idle,
                cpu.nice,
                cpu.user,
                cpu.iowait,
                cpu.system,
                cpu.irq,
                cpu.steal,
                100 - int(cpu.idle),
            )
            feature_key = '{0}-{1}'.format('cpu', idx)
            yield (feature_key, feature_attributes, 'cpu') 
開發者ID:cloudviz,項目名稱:agentless-system-crawler,代碼行數:18,代碼來源:cpu_host_crawler.py

示例4: test_serialization

# 需要導入模塊: import psutil [as 別名]
# 或者: from psutil import cpu_times_percent [as 別名]
def test_serialization(self):
        def check(ret):
            if json is not None:
                json.loads(json.dumps(ret))
            a = pickle.dumps(ret)
            b = pickle.loads(a)
            self.assertEqual(ret, b)

        check(psutil.Process().as_dict())
        check(psutil.virtual_memory())
        check(psutil.swap_memory())
        check(psutil.cpu_times())
        check(psutil.cpu_times_percent(interval=0))
        check(psutil.net_io_counters())
        if LINUX and not os.path.exists('/proc/diskstats'):
            pass
        else:
            if not APPVEYOR:
                check(psutil.disk_io_counters())
        check(psutil.disk_partitions())
        check(psutil.disk_usage(os.getcwd()))
        check(psutil.users()) 
開發者ID:birforce,項目名稱:vnpy_crypto,代碼行數:24,代碼來源:test_misc.py

示例5: __call__

# 需要導入模塊: import psutil [as 別名]
# 或者: from psutil import cpu_times_percent [as 別名]
def __call__(self):
        """ Returns a derived gauge for the processor time.

        Processor time is defined as a float representing the current system
        wide CPU utilization minus idle CPU time as a percentage. Idle CPU
        time is defined as the time spent doing nothing. Return values range
        from 0.0 to 100.0 inclusive.

        :rtype: :class:`opencensus.metrics.export.gauge.DerivedDoubleGauge`
        :return: The gauge representing the processor time metric
        """
        gauge = DerivedDoubleGauge(
            ProcessorTimeMetric.NAME,
            'Processor time as a percentage',
            'percentage',
            [])
        gauge.create_default_time_series(ProcessorTimeMetric.get_value)
        # From the psutil docs: the first time this method is called with
        # interval = None it will return a meaningless 0.0 value which you are
        # supposed to ignore. Call cpu_percent() once so that the subsequent
        # calls from the gauge will be meaningful.
        psutil.cpu_times_percent()
        return gauge 
開發者ID:census-instrumentation,項目名稱:opencensus-python,代碼行數:25,代碼來源:cpu.py

示例6: iowait

# 需要導入模塊: import psutil [as 別名]
# 或者: from psutil import cpu_times_percent [as 別名]
def iowait():
    cpu_percent = psutil.cpu_times_percent()
    try:
        return cpu_percent.iowait
    except AttributeError:
        return None 
開發者ID:mars-project,項目名稱:mars,代碼行數:8,代碼來源:resource.py

示例7: test_cpu_times_percent

# 需要導入模塊: import psutil [as 別名]
# 或者: from psutil import cpu_times_percent [as 別名]
def test_cpu_times_percent(self):
        last = psutil.cpu_times_percent(interval=0.001)
        for x in range(100):
            new = psutil.cpu_times_percent(interval=None)
            for percent in new:
                self._test_cpu_percent(percent, last, new)
            self._test_cpu_percent(sum(new), last, new)
            last = new 
開發者ID:birforce,項目名稱:vnpy_crypto,代碼行數:10,代碼來源:test_system.py

示例8: test_per_cpu_times_percent

# 需要導入模塊: import psutil [as 別名]
# 或者: from psutil import cpu_times_percent [as 別名]
def test_per_cpu_times_percent(self):
        last = psutil.cpu_times_percent(interval=0.001, percpu=True)
        self.assertEqual(len(last), psutil.cpu_count())
        for x in range(100):
            new = psutil.cpu_times_percent(interval=None, percpu=True)
            for cpu in new:
                for percent in cpu:
                    self._test_cpu_percent(percent, last, new)
                self._test_cpu_percent(sum(cpu), last, new)
            last = new 
開發者ID:birforce,項目名稱:vnpy_crypto,代碼行數:12,代碼來源:test_system.py

示例9: test_per_cpu_times_percent_negative

# 需要導入模塊: import psutil [as 別名]
# 或者: from psutil import cpu_times_percent [as 別名]
def test_per_cpu_times_percent_negative(self):
        # see: https://github.com/giampaolo/psutil/issues/645
        psutil.cpu_times_percent(percpu=True)
        zero_times = [x._make([0 for x in range(len(x._fields))])
                      for x in psutil.cpu_times(percpu=True)]
        with mock.patch('psutil.cpu_times', return_value=zero_times):
            for cpu in psutil.cpu_times_percent(percpu=True):
                for percent in cpu:
                    self._test_cpu_percent(percent, None, None) 
開發者ID:birforce,項目名稱:vnpy_crypto,代碼行數:11,代碼來源:test_system.py

示例10: test_cpu_times_percent

# 需要導入模塊: import psutil [as 別名]
# 或者: from psutil import cpu_times_percent [as 別名]
def test_cpu_times_percent(self):
        self.assert_ntuple_of_nums(psutil.cpu_times_percent(interval=None))
        self.assert_ntuple_of_nums(psutil.cpu_times_percent(interval=0.0001)) 
開發者ID:giampaolo,項目名稱:psutil,代碼行數:5,代碼來源:test_contracts.py

示例11: test_cpu_steal_decrease

# 需要導入模塊: import psutil [as 別名]
# 或者: from psutil import cpu_times_percent [as 別名]
def test_cpu_steal_decrease(self):
        # Test cumulative cpu stats decrease. We should ignore this.
        # See issue #1210.
        with mock_open_content(
            "/proc/stat",
            textwrap.dedent("""\
                cpu   0 0 0 0 0 0 0 1 0 0
                cpu0  0 0 0 0 0 0 0 1 0 0
                cpu1  0 0 0 0 0 0 0 1 0 0
                """).encode()) as m:
            # first call to "percent" functions should read the new stat file
            # and compare to the "real" file read at import time - so the
            # values are meaningless
            psutil.cpu_percent()
            assert m.called
            psutil.cpu_percent(percpu=True)
            psutil.cpu_times_percent()
            psutil.cpu_times_percent(percpu=True)

        with mock_open_content(
            "/proc/stat",
            textwrap.dedent("""\
                cpu   1 0 0 0 0 0 0 0 0 0
                cpu0  1 0 0 0 0 0 0 0 0 0
                cpu1  1 0 0 0 0 0 0 0 0 0
                """).encode()) as m:
            # Increase "user" while steal goes "backwards" to zero.
            cpu_percent = psutil.cpu_percent()
            assert m.called
            cpu_percent_percpu = psutil.cpu_percent(percpu=True)
            cpu_times_percent = psutil.cpu_times_percent()
            cpu_times_percent_percpu = psutil.cpu_times_percent(percpu=True)
            self.assertNotEqual(cpu_percent, 0)
            self.assertNotEqual(sum(cpu_percent_percpu), 0)
            self.assertNotEqual(sum(cpu_times_percent), 0)
            self.assertNotEqual(sum(cpu_times_percent), 100.0)
            self.assertNotEqual(sum(map(sum, cpu_times_percent_percpu)), 0)
            self.assertNotEqual(sum(map(sum, cpu_times_percent_percpu)), 100.0)
            self.assertEqual(cpu_times_percent.steal, 0)
            self.assertNotEqual(cpu_times_percent.user, 0) 
開發者ID:giampaolo,項目名稱:psutil,代碼行數:42,代碼來源:test_linux.py

示例12: init

# 需要導入模塊: import psutil [as 別名]
# 或者: from psutil import cpu_times_percent [as 別名]
def init(self):
        t = tp_timestamp_ms() - 10 * 60 * 1000
        cnt = int((10 * 60 + self._INTERVAL - 1) / self._INTERVAL)
        for i in range(cnt):
            val = {
                't': t,
                'cpu': {'u': 0, 's': 0},
                'mem': {'u': 0, 't': 100},
                'disk': {'r': 0, 'w': 0},
                'net': {'r': 0, 's': 0}
            }
            self._sys_stats.append(val)
            t += self._INTERVAL * 1000

        psutil.cpu_times_percent()
        net = psutil.net_io_counters(pernic=False)
        self._net_recv = net.bytes_recv
        self._net_sent = net.bytes_sent
        disk = psutil.disk_io_counters(perdisk=False)
        self._disk_read = disk.read_bytes
        self._disk_write = disk.write_bytes

        err, c = stats.get_basic_stats()
        if TPE_OK == err:
            self._counter_stats = c

        # 每 5秒 采集一次係統狀態統計數據
        tp_cron().add_job('sys_status', self._check_sys_stats, first_interval_seconds=self._INTERVAL, interval_seconds=self._INTERVAL)
        # 每 1小時 重新查詢一次數據庫,得到用戶數/主機數/賬號數/連接數,避免統計數量出現偏差
        tp_cron().add_job('query_counter', self._query_counter, first_interval_seconds=60 * 60, interval_seconds=60 * 60)
        # 每 1分鍾 檢查一下臨時鎖定用戶是否可以自動解鎖了
        tp_cron().add_job('check_temp_locked_user', self._check_temp_locked_user, interval_seconds=60)

        tp_wss().register_get_sys_status_callback(self.get_sys_stats)
        tp_wss().register_get_stat_counter_callback(self.get_counter_stats)

        return True 
開發者ID:tp4a,項目名稱:teleport,代碼行數:39,代碼來源:stats.py

示例13: _check_sys_stats

# 需要導入模塊: import psutil [as 別名]
# 或者: from psutil import cpu_times_percent [as 別名]
def _check_sys_stats(self):
        val = {'t': tp_timestamp_ms()}

        cpu = psutil.cpu_times_percent()
        val['cpu'] = {'u': cpu.user, 's': cpu.system}

        mem = psutil.virtual_memory()
        val['mem'] = {'u': mem.used, 't': mem.total}

        disk = psutil.disk_io_counters(perdisk=False)
        _read = disk.read_bytes - self._disk_read
        _write = disk.write_bytes - self._disk_write
        self._disk_read = disk.read_bytes
        self._disk_write = disk.write_bytes

        if _read < 0:
            _read = 0
        if _write < 0:
            _write = 0
        val['disk'] = {'r': int(_read / self._INTERVAL), 'w': int(_write / self._INTERVAL)}

        net = psutil.net_io_counters(pernic=False)
        _recv = net.bytes_recv - self._net_recv
        _sent = net.bytes_sent - self._net_sent
        self._net_recv = net.bytes_recv
        self._net_sent = net.bytes_sent

        # On some systems such as Linux, on a very busy or long-lived system, the numbers
        # returned by the kernel may overflow and wrap (restart from zero)
        if _recv < 0:
            _recv = 0
        if _sent < 0:
            _sent = 0
        val['net'] = {'r': int(_recv / self._INTERVAL), 's': int(_sent / self._INTERVAL)}

        self._sys_stats.pop(0)
        self._sys_stats.append(val)

        tp_wss().send_message('sys_status', val) 
開發者ID:tp4a,項目名稱:teleport,代碼行數:41,代碼來源:stats.py

示例14: get_system_load

# 需要導入模塊: import psutil [as 別名]
# 或者: from psutil import cpu_times_percent [as 別名]
def get_system_load(self):
        return {
            MEM_AVAILABLE: psutil.virtual_memory().available,
            CPU_IDLE_PERCENT: psutil.cpu_times_percent(
                self.cpu_sample_interval).idle,
            CLIENT_NUMBER: len(self.protocol.server.clients),
        } 
開發者ID:bsc-s2,項目名稱:pykit,代碼行數:9,代碼來源:wsjobd.py

示例15: __get_cpu_usage

# 需要導入模塊: import psutil [as 別名]
# 或者: from psutil import cpu_times_percent [as 別名]
def __get_cpu_usage(self):
        return (100.0 - psutil.cpu_times_percent(interval=0.1).idle) / 100.0 
開發者ID:RUB-SysSec,項目名稱:kAFL,代碼行數:4,代碼來源:ui.py


注:本文中的psutil.cpu_times_percent方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。