精品伊人久久大香线蕉,开心久久婷婷综合中文字幕,杏田冲梨,人妻无码aⅴ不卡中文字幕

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
win/linux 下使用 psutil 獲取進(jìn)程 CPU / memory / IO 占用信息

psutil - A cross-platform process and system utilities module for Python

1. 安裝

pip 安裝即可。

windows 下需要安裝 vs2008,否則報(bào)錯(cuò):?Unable to find vcvarsall.bat?

如果已經(jīng)安裝 vs2010 / vs2012 則需要設(shè)置環(huán)境變量,VS90COMNTOOLS 指向已有的 vs 變量。

vs2010 設(shè)置如下:

VS90COMNTOOLS = %VS100COMNTOOLS%

2. 獲取特定進(jìn)程對(duì)象

  • 根據(jù)進(jìn)程 ID 創(chuàng)建進(jìn)程對(duì)象
  • 獲取所有進(jìn)程對(duì)象,過濾出目標(biāo)進(jìn)程
# -*- coding: utf-8-*-import psutildef get_proc_by_id(pid):    return psutil.Process(pid)def get_proc_by_name(pname):    """ get process by name        return the first process if there are more than one    """    for proc in psutil.process_iter():        try:            if proc.name().lower() == pname.lower():                return proc  # return if found one        except psutil.AccessDenied:            pass        except psutil.NoSuchProcess:            pass    return Noneif '__main__' == __name__:    print get_proc_by_name("chrome.exe")    print get_proc_by_id(4364)

3. 獲取進(jìn)程信息

3.1?需要特別注意異常保護(hù),尤其是??psutil.AccessDenied

不同的進(jìn)程,權(quán)限等信息可能不同,遍歷所有進(jìn)程取信息時(shí),需要對(duì)每一個(gè)進(jìn)程單獨(dú)進(jìn)程異常保護(hù)。

3.2 獲取所有進(jìn)程

大多數(shù) demo 代碼中,都是使用??psutil.get_process_list?,但該方法在源碼中已經(jīng)標(biāo)記為廢棄。

新推薦的是??psutil.process_iter?迭代器。

根據(jù)下面的源碼可知實(shí)現(xiàn)原理:獲取所有進(jìn)程 ID,然后根據(jù) ID 創(chuàng)建進(jìn)程對(duì)象。

_pmap = {}def process_iter():    """Return a generator yielding a Process class instance for all    running processes on the local machine.    Every new Process instance is only created once and then cached    into an internal table which is updated every time this is used.    Cached Process instances are checked for identity so that you're    safe in case a PID has been reused by another process, in which    case the cached instance is updated.    The sorting order in which processes are yielded is based on    their PIDs.    """    def add(pid):        proc = Process(pid)        _pmap[proc.pid] = proc        return proc    def remove(pid):        _pmap.pop(pid, None)    a = set(get_pid_list())    b = set(_pmap.keys())    new_pids = a - b    gone_pids = b - a    for pid in gone_pids:        remove(pid)    for pid, proc in sorted(list(_pmap.items())                               list(dict.fromkeys(new_pids).items())):        try:            if proc is None:  # new process                yield add(pid)            else:                # use is_running() to check whether PID has been reused by                # another process in which case yield a new Process instance                if proc.is_running():                    yield proc                else:                    yield add(pid)        except NoSuchProcess:            remove(pid)        except AccessDenied:            # Process creation time can't be determined hence there's            # no way to tell whether the pid of the cached process            # has been reused. Just return the cached version.            yield proc@_deprecated()def get_process_list():    """Return a list of Process class instances for all running    processes on the local machine (deprecated).    """    return list(process_iter())

3.3 進(jìn)程的內(nèi)存信息 --?VSS/RSS/PSS/USS

VSS 是剩余的可訪問內(nèi)存。

進(jìn)程占用內(nèi)存包括 2 部分,自身 共享庫。不同的算法產(chǎn)生了 3 個(gè)不同的內(nèi)存指標(biāo),分別是:RSS / PSS / USS。

一般來說內(nèi)存占用大小有如下規(guī)律:VSS >= RSS >= PSS >= USS

?

Demo 代碼如下

proc = psutil.Process(4364)total = psutil.virtual_memory().totalrss, vss = proc.memory_info()percent = proc.memory_percent()print "rss: %s Byte, vss: %s Byte" % (rss, vss)print "total: %.2f(M)" % (float(total)/1024/1024/1024)print "percent: %.2f%%, calc: %.2f%%" % (percent, 100*float(rss)/total)

輸出

本機(jī)內(nèi)存信息截圖

詳細(xì)說明:

  • VSS(reported as VSZ from ps) is?the total accessible address space of a process.?
    This size also includes memory that may not be resident in RAM like mallocs that have been allocated but not written to.
    VSS is of very little use for determing real memory usage of a process.
  • RSS?is the?total memory actually held in RAM for a process.
    RSS can be misleading, because it reports the total all of the shared libraries that the process uses,
    even though a shared library is only loaded into memory once regardless of how many processes use it.
    RSS is not an accurate representation of the memory usage for a single process.
  • PSS?differs from RSS in that it reports the proportional size of its shared libraries,
    i.e. if three processes all use a shared library that has 30 pages,
    that library will only contribute 10 pages to the PSS that is reported for each of the three processes.
    PSS is a very useful number because when the PSS for all processes in the system are summed together,
    that is a good representation for the total memory usage in the system.
    When a process is killed, the shared libraries that contributed to its PSS
    will be proportionally distributed to the PSS totals for the remaining processes still using that library.
    In this way PSS can be slightly misleading, because
    when a process is killed, PSS does not accurately represent the memory returned to the overall system.
  • USS?is?the total private memory for a process,
    i.e. that memory that is completely unique to that process.
    USS is an extremely useful number because it indicates the true incremental cost of running a particular process.
    When a process is killed, the USS is the total memory that is actually returned to the system.
    USS is the best number to watch when initially suspicious ofmemory leaksin a process.

轉(zhuǎn)載于:https://www.cnblogs.com/misspy/p/3851327.html

來源:https://www.icode9.com/content-3-271401.html
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Android 終端性能測試——內(nèi)存篇
使用procrank分析內(nèi)存利用及分析源代碼
LinuxProcessMemoryUsage
Android內(nèi)存之VSS/RSS/PSS/USS
Python 監(jiān)控腳本(硬盤、cpu、內(nèi)存、網(wǎng)卡、進(jìn)程)
python 模塊psutil獲取進(jìn)程信息
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

主站蜘蛛池模板: 北流市| 嘉义县| 青田县| 公主岭市| 吴桥县| 德保县| 德钦县| 息烽县| 利川市| 平湖市| 海门市| 诏安县| 莱西市| 华亭县| 汉中市| 汪清县| 常德市| 和平县| 祁连县| 长岭县| 邵东县| 尖扎县| 玉门市| 奉贤区| 大方县| 油尖旺区| 云霄县| 大余县| 杨浦区| 资阳市| 阳城县| 金山区| 湾仔区| 湘潭县| 班玛县| 扎兰屯市| 开远市| 玉溪市| 通城县| 延庆县| 绍兴市|