system_info.py 772 字节
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2020-02-16 11:24
# @Author  : Lemon
# @File    : running_info.py
# @Software: PyCharm


import psutil
import os, datetime


def _get_delta(timestamp):
    t = str(datetime.datetime.now() - datetime.datetime.fromtimestamp(timestamp))
    try:
        t = t[:-7]
    except:
        pass
    return t


def get():
    data = psutil.virtual_memory()
    memory = "%d" % (int(round(data.percent))) + "%"
    cpu = "%0.2f" % psutil.cpu_percent(1) + "%"

    p = psutil.Process(os.getpid())
    process_time = _get_delta(p.create_time())
    boot_time = _get_delta(psutil.boot_time())
    return {
        'memory': memory,
        'cpu': cpu,
        'process_time': process_time,
        'boot_time': boot_time
    }