zoukankan      html  css  js  c++  java
  • ctypes运用

    # _*_ coding: utf-8 _*_
    __author__ = 'pythonwu'
    __date__ = "2018/10/18 17:29"

    __all__ = ['get_current_process', 'get_memory_info', 'get_memory_usage']

    import ctypes
    from ctypes import wintypes

    GetCurrentProcess = ctypes.windll.kernel32.GetCurrentProcess
    GetCurrentProcess.argtypes = []
    GetCurrentProcess.restype = wintypes.HANDLE

    SIZE_T = ctypes.c_size_t

    class PROCESS_MEMORY_COUNTERS_EX(ctypes.Structure):
    _fields_ = [
    ('cb', wintypes.DWORD),
    ('PageFaultCount', wintypes.DWORD),
    ('PeakWorkingSetSize', SIZE_T),
    ('WorkingSetSize', SIZE_T),
    ('QuotaPeakPagedPoolUsage', SIZE_T),
    ('QuotaPagedPoolUsage', SIZE_T),
    ('QuotaPeakNonPagedPoolUsage', SIZE_T),
    ('QuotaNonPagedPoolUsage', SIZE_T),
    ('PagefileUsage', SIZE_T),
    ('PeakPagefileUsage', SIZE_T),
    ('PrivateUsage', SIZE_T),
    ]

    GetProcessMemoryInfo = ctypes.windll.psapi.GetProcessMemoryInfo
    GetProcessMemoryInfo.argtypes = [
    wintypes.HANDLE,
    ctypes.POINTER(PROCESS_MEMORY_COUNTERS_EX),
    wintypes.DWORD,
    ]
    GetProcessMemoryInfo.restype = wintypes.BOOL

    def get_current_process():
    """Return handle to current process."""
    return GetCurrentProcess()

    def get_memory_info(process=None):
    """Return Win32 process memory counters structure as a dict."""
    if process is None:
    process = get_current_process()
    counters = PROCESS_MEMORY_COUNTERS_EX()
    ret = GetProcessMemoryInfo(process, ctypes.byref(counters),
    ctypes.sizeof(counters))
    if not ret:
    raise ctypes.WinError()
    info = dict((name, getattr(counters, name))
    for name, _ in counters._fields_)
    return info

    def get_memory_usage(process=None):
    """Return this process's memory usage in bytes."""
    info = get_memory_info(process=process)
    return info['PrivateUsage']

    if __name__ == '__main__':
    import pprint
    pprint.pprint(get_memory_info())

    {'PageFaultCount': 2916,
    'PagefileUsage': 4583424,
    'PeakPagefileUsage': 4583424,
    'PeakWorkingSetSize': 11063296,
    'PrivateUsage': 4583424,
    'QuotaNonPagedPoolUsage': 11344,
    'QuotaPagedPoolUsage': 140064,
    'QuotaPeakNonPagedPoolUsage': 11472,
    'QuotaPeakPagedPoolUsage': 140064,
    'WorkingSetSize': 11063296,
    'cb': 44}

  • 相关阅读:
    设计模式学习笔记--迭代器模式
    设计模式学习笔记--组合模式
    设计模式学习笔记--备忘录模式
    Asp.Net Core IdentityServer4 中的基本概念
    Asp.Net Core 中间件应用实践中你不知道的那些事
    Asp.Net Core Filter 深入浅出的那些事-AOP
    ASP.NET CORE 内置的IOC解读及使用
    ASP.NET CORE 管道模型及中间件使用解读
    ASP.NET CORE 启动过程及源码解读
    Linux +Docker +Nginx 部署代理转发初探
  • 原文地址:https://www.cnblogs.com/wudeng/p/9812682.html
Copyright © 2011-2022 走看看