{"id":49227,"date":"2025-07-30T16:36:42","date_gmt":"2025-07-30T08:36:42","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/49227.html"},"modified":"2025-07-30T16:36:42","modified_gmt":"2025-07-30T08:36:42","slug":"python%e7%b3%bb%e7%bb%9f%e7%9b%91%e6%8e%a7%e5%88%a9%e5%99%a8%ef%bc%9apsutil%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/49227.html","title":{"rendered":"Python\u7cfb\u7edf\u76d1\u63a7\u5229\u5668\uff1apsutil\u8be6\u89e3"},"content":{"rendered":"<h3>\u5f15\u8a00<\/h3>\n<p>psutil(Python system and process utilities)\u662f\u4e00\u4e2a\u8de8\u5e73\u53f0\u7684Python\u5e93&#xff0c;\u5b83\u5141\u8bb8\u5f00\u53d1\u4eba\u5458\u8f7b\u677e\u83b7\u53d6\u7cfb\u7edf\u8fd0\u884c\u65f6\u7684\u5404\u79cd\u4fe1\u606f\u3002\u8fd9\u4e2a\u5e93\u6700\u521d\u7531Giampaolo Rodola\u57282009\u5e74\u521b\u5efa&#xff0c;\u73b0\u5728\u5df2\u7ecf\u6210\u4e3aPython\u751f\u6001\u7cfb\u7edf\u4e2d\u7cfb\u7edf\u76d1\u63a7\u9886\u57df\u7684\u6807\u51c6\u5de5\u5177\u4e4b\u4e00\u3002psutil\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u5355\u800c\u7edf\u4e00\u7684\u65b9\u5f0f\u6765\u83b7\u53d6\u7cfb\u7edf\u5229\u7528\u7387\u4fe1\u606f(CPU\u3001\u5185\u5b58\u3001\u78c1\u76d8\u3001\u7f51\u7edc)\u548c\u8fdb\u7a0b\u7ba1\u7406\u529f\u80fd&#xff0c;\u4e0d\u9700\u8981\u4f9d\u8d56\u7cfb\u7edf\u7279\u5b9a\u7684\u547d\u4ee4\u884c\u5de5\u5177\u3002<\/p>\n<p>\u5728\u7cfb\u7edf\u76d1\u63a7\u548c\u8d44\u6e90\u7ba1\u7406\u9886\u57df&#xff0c;psutil\u626e\u6f14\u7740\u81f3\u5173\u91cd\u8981\u7684\u89d2\u8272\u3002\u5b83\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u4eba\u5458&#xff1a;<\/p>\n<ul>\n<li>\u5b9e\u65f6\u76d1\u63a7\u670d\u52a1\u5668\u8d44\u6e90\u4f7f\u7528\u60c5\u51b5<\/li>\n<li>\u8bca\u65ad\u7cfb\u7edf\u6027\u80fd\u74f6\u9888<\/li>\n<li>\u81ea\u52a8\u7ba1\u7406\u8fdb\u7a0b\u751f\u547d\u5468\u671f<\/li>\n<li>\u6784\u5efa\u81ea\u5b9a\u4e49\u7684\u7cfb\u7edf\u76d1\u63a7\u5de5\u5177<\/li>\n<li>\u5b9e\u73b0\u81ea\u52a8\u5316\u8fd0\u7ef4\u4efb\u52a1<\/li>\n<\/ul>\n<h3>\u5b89\u88c5\u4e0e\u57fa\u672c\u914d\u7f6e<\/h3>\n<h4>\u5b89\u88c5\u65b9\u6cd5<\/h4>\n<p>psutil\u53ef\u4ee5\u901a\u8fc7Python\u7684\u5305\u7ba1\u7406\u5de5\u5177pip\u8f7b\u677e\u5b89\u88c5&#xff1a;<\/p>\n<p>pip install psutil<\/p>\n<p>\u5bf9\u4e8e\u7279\u5b9a\u7248\u672c\u7684\u9700\u6c42&#xff0c;\u53ef\u4ee5\u6307\u5b9a\u7248\u672c\u53f7\u5b89\u88c5&#xff1a;<\/p>\n<p>pip install psutil&#061;&#061;5.9.0<\/p>\n<h4>\u517c\u5bb9\u6027\u8bf4\u660e<\/h4>\n<p>psutil\u5177\u6709\u51fa\u8272\u7684\u8de8\u5e73\u53f0\u517c\u5bb9\u6027&#xff0c;\u652f\u6301\u4ee5\u4e0b\u64cd\u4f5c\u7cfb\u7edf&#xff1a;<\/p>\n<ul>\n<li>Windows (32\u4f4d\u548c64\u4f4d)<\/li>\n<li>Linux (\u5404\u4e3b\u6d41\u53d1\u884c\u7248)<\/li>\n<li>macOS<\/li>\n<li>FreeBSD, OpenBSD, NetBSD<\/li>\n<li>Sun Solaris<\/li>\n<li>AIX<\/li>\n<\/ul>\n<p>\u652f\u6301\u7684Python\u7248\u672c\u5305\u62ec&#xff1a;<\/p>\n<ul>\n<li>Python 2.7 (psutil 5.x\u662f\u6700\u540e\u4e00\u4e2a\u652f\u6301Python 2\u7684\u7248\u672c)<\/li>\n<li>Python 3.4&#043;<\/li>\n<li>PyPy<\/li>\n<\/ul>\n<h3>\u6838\u5fc3\u529f\u80fd\u4e0e\u7528\u6cd5<\/h3>\n<h4>\u83b7\u53d6\u7cfb\u7edf\u4fe1\u606f<\/h4>\n<h5>CPU\u4f7f\u7528\u7387\u76d1\u63a7<\/h5>\n<p>psutil\u63d0\u4f9b\u4e86\u591a\u79cdCPU\u76d1\u63a7\u529f\u80fd&#xff1a;<\/p>\n<p>import psutil<\/p>\n<p># \u83b7\u53d6CPU\u903b\u8f91\u6838\u5fc3\u6570<br \/>\nprint(f&#034;\u903b\u8f91CPU\u6838\u5fc3\u6570: {psutil.cpu_count()}&#034;)<\/p>\n<p># \u83b7\u53d6CPU\u7269\u7406\u6838\u5fc3\u6570(\u4e0d\u5305\u62ec\u8d85\u7ebf\u7a0b)<br \/>\nprint(f&#034;\u7269\u7406CPU\u6838\u5fc3\u6570: {psutil.cpu_count(logical&#061;False)}&#034;)<\/p>\n<p># \u83b7\u53d6CPU\u4f7f\u7528\u7387(\u963b\u585e1\u79d2)<br \/>\nprint(f&#034;CPU\u4f7f\u7528\u7387: {psutil.cpu_percent(interval&#061;1)}%&#034;)<\/p>\n<p># \u83b7\u53d6\u6bcf\u4e2aCPU\u6838\u5fc3\u7684\u4f7f\u7528\u7387<br \/>\nprint(f&#034;\u5404\u6838\u5fc3\u4f7f\u7528\u7387: {psutil.cpu_percent(interval&#061;1, percpu&#061;True)}&#034;)<\/p>\n<p># \u83b7\u53d6CPU\u9891\u7387\u4fe1\u606f<br \/>\ncpu_freq &#061; psutil.cpu_freq()<br \/>\nprint(f&#034;\u5f53\u524d\u9891\u7387: {cpu_freq.current}MHz&#034;)<br \/>\nprint(f&#034;\u6700\u5c0f\u9891\u7387: {cpu_freq.min}MHz&#034;)<br \/>\nprint(f&#034;\u6700\u5927\u9891\u7387: {cpu_freq.max}MHz&#034;)<\/p>\n<p># \u83b7\u53d6CPU\u7edf\u8ba1\u4fe1\u606f(\u4e0a\u4e0b\u6587\u5207\u6362\u3001\u4e2d\u65ad\u7b49)<br \/>\nprint(f&#034;CPU\u7edf\u8ba1: {psutil.cpu_stats()}&#034;)<\/p>\n<h5>\u5185\u5b58\u4f7f\u7528\u60c5\u51b5<\/h5>\n<p>psutil\u53ef\u4ee5\u8be6\u7ec6\u76d1\u63a7\u7cfb\u7edf\u5185\u5b58\u4f7f\u7528\u60c5\u51b5&#xff1a;<\/p>\n<p># \u83b7\u53d6\u7269\u7406\u5185\u5b58\u4fe1\u606f<br \/>\nmem &#061; psutil.virtual_memory()<br \/>\nprint(f&#034;\u603b\u5185\u5b58: {mem.total\/1024\/1024:.2f} MB&#034;)<br \/>\nprint(f&#034;\u53ef\u7528\u5185\u5b58: {mem.available\/1024\/1024:.2f} MB&#034;)<br \/>\nprint(f&#034;\u5df2\u7528\u5185\u5b58: {mem.used\/1024\/1024:.2f} MB&#034;)<br \/>\nprint(f&#034;\u5185\u5b58\u4f7f\u7528\u7387: {mem.percent}%&#034;)<\/p>\n<p># \u83b7\u53d6\u4ea4\u6362\u5185\u5b58\u4fe1\u606f<br \/>\nswap &#061; psutil.swap_memory()<br \/>\nprint(f&#034;\u4ea4\u6362\u5185\u5b58\u603b\u91cf: {swap.total\/1024\/1024:.2f} MB&#034;)<br \/>\nprint(f&#034;\u5df2\u7528\u4ea4\u6362\u5185\u5b58: {swap.used\/1024\/1024:.2f} MB&#034;)<br \/>\nprint(f&#034;\u4ea4\u6362\u5185\u5b58\u4f7f\u7528\u7387: {swap.percent}%&#034;)<\/p>\n<h5>\u78c1\u76d8I\/O\u548c\u5206\u533a\u4fe1\u606f<\/h5>\n<p>\u78c1\u76d8\u76d1\u63a7\u529f\u80fd\u793a\u4f8b&#xff1a;<\/p>\n<p># \u83b7\u53d6\u78c1\u76d8\u5206\u533a\u4fe1\u606f<br \/>\npartitions &#061; psutil.disk_partitions()<br \/>\nfor partition in partitions:<br \/>\n    print(f&#034;\u8bbe\u5907: {partition.device}&#034;)<br \/>\n    print(f&#034;\u6302\u8f7d\u70b9: {partition.mountpoint}&#034;)<br \/>\n    print(f&#034;\u6587\u4ef6\u7cfb\u7edf\u7c7b\u578b: {partition.fstype}&#034;)<\/p>\n<p>    # \u83b7\u53d6\u78c1\u76d8\u4f7f\u7528\u60c5\u51b5<br \/>\n    usage &#061; psutil.disk_usage(partition.mountpoint)<br \/>\n    print(f&#034;\u603b\u7a7a\u95f4: {usage.total\/1024\/1024:.2f} MB&#034;)<br \/>\n    print(f&#034;\u5df2\u7528\u7a7a\u95f4: {usage.used\/1024\/1024:.2f} MB&#034;)<br \/>\n    print(f&#034;\u53ef\u7528\u7a7a\u95f4: {usage.free\/1024\/1024:.2f} MB&#034;)<br \/>\n    print(f&#034;\u4f7f\u7528\u7387: {usage.percent}%&#034;)<\/p>\n<p># \u83b7\u53d6\u78c1\u76d8IO\u7edf\u8ba1<br \/>\ndisk_io &#061; psutil.disk_io_counters()<br \/>\nprint(f&#034;\u8bfb\u53d6\u6b21\u6570: {disk_io.read_count}&#034;)<br \/>\nprint(f&#034;\u5199\u5165\u6b21\u6570: {disk_io.write_count}&#034;)<br \/>\nprint(f&#034;\u8bfb\u53d6\u5b57\u8282\u6570: {disk_io.read_bytes\/1024\/1024:.2f} MB&#034;)<br \/>\nprint(f&#034;\u5199\u5165\u5b57\u8282\u6570: {disk_io.write_bytes\/1024\/1024:.2f} MB&#034;)<\/p>\n<h5>\u7f51\u7edc\u63a5\u53e3\u7edf\u8ba1<\/h5>\n<p>\u7f51\u7edc\u76d1\u63a7\u529f\u80fd&#xff1a;<\/p>\n<p># \u83b7\u53d6\u7f51\u7edc\u63a5\u53e3\u4fe1\u606f<br \/>\nnet_if &#061; psutil.net_if_addrs()<br \/>\nfor interface, addrs in net_if.items():<br \/>\n    print(f&#034;\u63a5\u53e3: {interface}&#034;)<br \/>\n    for addr in addrs:<br \/>\n        print(f&#034;  IP\u5730\u5740: {addr.address}&#034;)<br \/>\n        print(f&#034;  \u7f51\u7edc\u63a9\u7801: {addr.netmask}&#034;)<br \/>\n        print(f&#034;  \u5e7f\u64ad\u5730\u5740: {addr.broadcast}&#034;)<\/p>\n<p># \u83b7\u53d6\u7f51\u7edcIO\u7edf\u8ba1<br \/>\nnet_io &#061; psutil.net_io_counters()<br \/>\nprint(f&#034;\u53d1\u9001\u5b57\u8282\u6570: {net_io.bytes_sent\/1024:.2f} KB&#034;)<br \/>\nprint(f&#034;\u63a5\u6536\u5b57\u8282\u6570: {net_io.bytes_recv\/1024:.2f} KB&#034;)<br \/>\nprint(f&#034;\u53d1\u9001\u6570\u636e\u5305\u6570: {net_io.packets_sent}&#034;)<br \/>\nprint(f&#034;\u63a5\u6536\u6570\u636e\u5305\u6570: {net_io.packets_recv}&#034;)<\/p>\n<p># \u83b7\u53d6\u5f53\u524d\u7f51\u7edc\u8fde\u63a5<br \/>\nconnections &#061; psutil.net_connections()<br \/>\nfor conn in connections:<br \/>\n    print(f&#034;\u534f\u8bae: {conn.type}&#034;)<br \/>\n    print(f&#034;\u672c\u5730\u5730\u5740: {conn.laddr}&#034;)<br \/>\n    print(f&#034;\u8fdc\u7a0b\u5730\u5740: {conn.raddr}&#034;)<br \/>\n    print(f&#034;\u72b6\u6001: {conn.status}&#034;)<\/p>\n<h4>\u8fdb\u7a0b\u7ba1\u7406<\/h4>\n<h5>\u5217\u51fa\u5f53\u524d\u8fd0\u884c\u7684\u6240\u6709\u8fdb\u7a0b<\/h5>\n<p># \u83b7\u53d6\u6240\u6709\u8fdb\u7a0bID<br \/>\npids &#061; psutil.pids()<\/p>\n<p># \u904d\u5386\u6240\u6709\u8fdb\u7a0b<br \/>\nfor proc in psutil.process_iter([&#039;pid&#039;, &#039;name&#039;, &#039;username&#039;]):<br \/>\n    try:<br \/>\n        # \u83b7\u53d6\u8fdb\u7a0b\u8be6\u60c5<br \/>\n        pinfo &#061; proc.as_dict(attrs&#061;[&#039;pid&#039;, &#039;name&#039;, &#039;cpu_percent&#039;, &#039;memory_percent&#039;])<br \/>\n        print(pinfo)<br \/>\n    except psutil.NoSuchProcess:<br \/>\n        pass<\/p>\n<h5>\u83b7\u53d6\u7279\u5b9a\u8fdb\u7a0b\u7684\u8be6\u7ec6\u4fe1\u606f<\/h5>\n<p># \u901a\u8fc7PID\u83b7\u53d6\u8fdb\u7a0b\u5bf9\u8c61<br \/>\npid &#061; 1234  # \u66ff\u6362\u4e3a\u5b9e\u9645PID<br \/>\ntry:<br \/>\n    p &#061; psutil.Process(pid)<\/p>\n<p>    # \u83b7\u53d6\u8fdb\u7a0b\u57fa\u672c\u4fe1\u606f<br \/>\n    print(f&#034;\u8fdb\u7a0b\u540d: {p.name()}&#034;)<br \/>\n    print(f&#034;\u6267\u884c\u8def\u5f84: {p.exe()}&#034;)<br \/>\n    print(f&#034;\u5de5\u4f5c\u76ee\u5f55: {p.cwd()}&#034;)<br \/>\n    print(f&#034;\u542f\u52a8\u547d\u4ee4: {p.cmdline()}&#034;)<br \/>\n    print(f&#034;\u542f\u52a8\u65f6\u95f4: {p.create_time()}&#034;)<br \/>\n    print(f&#034;\u7528\u6237\u540d: {p.username()}&#034;)<\/p>\n<p>    # \u83b7\u53d6\u8d44\u6e90\u4f7f\u7528\u60c5\u51b5<br \/>\n    print(f&#034;CPU\u4f7f\u7528\u7387: {p.cpu_percent(interval&#061;1.0)}%&#034;)<br \/>\n    print(f&#034;\u5185\u5b58\u4f7f\u7528\u7387: {p.memory_percent()}%&#034;)<br \/>\n    mem_info &#061; p.memory_info()<br \/>\n    print(f&#034;\u5e38\u9a7b\u5185\u5b58: {mem_info.rss\/1024\/1024:.2f} MB&#034;)<br \/>\n    print(f&#034;\u865a\u62df\u5185\u5b58: {mem_info.vms\/1024\/1024:.2f} MB&#034;)<\/p>\n<p>    # \u83b7\u53d6\u7ebf\u7a0b\u4fe1\u606f<br \/>\n    print(f&#034;\u7ebf\u7a0b\u6570: {p.num_threads()}&#034;)<\/p>\n<p>    # \u83b7\u53d6\u6253\u5f00\u7684\u6587\u4ef6<br \/>\n    print(f&#034;\u6253\u5f00\u7684\u6587\u4ef6: {p.open_files()}&#034;)<\/p>\n<p>    # \u83b7\u53d6\u7f51\u7edc\u8fde\u63a5<br \/>\n    print(f&#034;\u7f51\u7edc\u8fde\u63a5: {p.connections()}&#034;)<\/p>\n<p>except psutil.NoSuchProcess:<br \/>\n    print(f&#034;\u8fdb\u7a0b {pid} \u4e0d\u5b58\u5728&#034;)<\/p>\n<h5>\u8fdb\u7a0b\u7684\u542f\u52a8\u3001\u7ec8\u6b62\u4e0e\u7ba1\u7406<\/h5>\n<p># \u542f\u52a8\u65b0\u8fdb\u7a0b<br \/>\nimport subprocess<br \/>\nproc &#061; subprocess.Popen([&#034;python&#034;, &#034;script.py&#034;])<br \/>\npsutil_proc &#061; psutil.Process(proc.pid)<\/p>\n<p># \u7ec8\u6b62\u8fdb\u7a0b<br \/>\npsutil_proc.terminate()  # \u4f18\u96c5\u7ec8\u6b62<br \/>\npsutil_proc.kill()      # \u5f3a\u5236\u7ec8\u6b62<\/p>\n<p># \u6302\u8d77\u548c\u6062\u590d\u8fdb\u7a0b<br \/>\npsutil_proc.suspend()<br \/>\npsutil_proc.resume()<\/p>\n<p># \u7b49\u5f85\u8fdb\u7a0b\u7ed3\u675f<br \/>\npsutil_proc.wait(timeout&#061;10)<\/p>\n<h3>\u7cfb\u7edf\u6027\u80fd\u4f18\u5316\u4e0e\u76d1\u63a7<\/h3>\n<h4>\u5b9e\u65f6\u76d1\u63a7\u7cfb\u7edf\u8d44\u6e90<\/h4>\n<p>import time<\/p>\n<p>def monitor_system(interval&#061;1):<br \/>\n    &#034;&#034;&#034;\u5b9e\u65f6\u76d1\u63a7\u7cfb\u7edf\u8d44\u6e90&#034;&#034;&#034;<br \/>\n    while True:<br \/>\n        # CPU\u4f7f\u7528\u7387<br \/>\n        cpu_percent &#061; psutil.cpu_percent(interval&#061;interval, percpu&#061;True)<\/p>\n<p>        # \u5185\u5b58\u4f7f\u7528\u60c5\u51b5<br \/>\n        mem &#061; psutil.virtual_memory()<\/p>\n<p>        # \u78c1\u76d8\u4f7f\u7528\u60c5\u51b5<br \/>\n        disk &#061; psutil.disk_usage(&#039;\/&#039;)<\/p>\n<p>        # \u7f51\u7edcIO<br \/>\n        net_io &#061; psutil.net_io_counters()<\/p>\n<p>        # \u6253\u5370\u76d1\u63a7\u4fe1\u606f<br \/>\n        print(&#034;\\\\n&#034; &#043; &#034;&#061;&#034;*50)<br \/>\n        print(f&#034;\u65f6\u95f4: {time.strftime(&#039;%Y-%m-%d %H:%M:%S&#039;)}&#034;)<br \/>\n        print(f&#034;CPU\u4f7f\u7528\u7387: {cpu_percent}&#034;)<br \/>\n        print(f&#034;\u5185\u5b58\u4f7f\u7528: {mem.percent}%&#034;)<br \/>\n        print(f&#034;\u78c1\u76d8\u4f7f\u7528: {disk.percent}%&#034;)<br \/>\n        print(f&#034;\u7f51\u7edc: \u4e0a\u4f20 {net_io.bytes_sent\/1024:.2f}KB, \u4e0b\u8f7d {net_io.bytes_recv\/1024:.2f}KB&#034;)<\/p>\n<p>        time.sleep(interval)<\/p>\n<p># monitor_system()<\/p>\n<h4>\u751f\u6210\u8d44\u6e90\u4f7f\u7528\u62a5\u544a<\/h4>\n<p>def generate_report(duration&#061;60, interval&#061;5):<br \/>\n    &#034;&#034;&#034;\u751f\u6210\u7cfb\u7edf\u8d44\u6e90\u4f7f\u7528\u62a5\u544a&#034;&#034;&#034;<br \/>\n    records &#061; []<br \/>\n    end_time &#061; time.time() &#043; duration<\/p>\n<p>    while time.time() &lt; end_time:<br \/>\n        record &#061; {<br \/>\n            &#039;timestamp&#039;: time.time(),<br \/>\n            &#039;cpu&#039;: psutil.cpu_percent(interval&#061;interval),<br \/>\n            &#039;memory&#039;: psutil.virtual_memory().percent,<br \/>\n            &#039;disk&#039;: psutil.disk_usage(&#039;\/&#039;).percent,<br \/>\n            &#039;network_sent&#039;: psutil.net_io_counters().bytes_sent,<br \/>\n            &#039;network_recv&#039;: psutil.net_io_counters().bytes_recv<br \/>\n        }<br \/>\n        records.append(record)<\/p>\n<p>    # \u8ba1\u7b97\u7edf\u8ba1\u6570\u636e<br \/>\n    cpu_avg &#061; sum(r[&#039;cpu&#039;] for r in records) \/ len(records)<br \/>\n    mem_avg &#061; sum(r[&#039;memory&#039;] for r in records) \/ len(records)<\/p>\n<p>    report &#061; {<br \/>\n        &#039;duration&#039;: duration,<br \/>\n        &#039;cpu_avg&#039;: cpu_avg,<br \/>\n        &#039;mem_avg&#039;: mem_avg,<br \/>\n        &#039;peak_cpu&#039;: max(r[&#039;cpu&#039;] for r in records),<br \/>\n        &#039;peak_mem&#039;: max(r[&#039;memory&#039;] for r in records),<br \/>\n        &#039;network_total&#039;: (records[-1][&#039;network_sent&#039;] &#8211; records[0][&#039;network_sent&#039;] &#043;<br \/>\n                         records[-1][&#039;network_recv&#039;] &#8211; records[0][&#039;network_recv&#039;]) \/ 1024<br \/>\n    }<\/p>\n<p>    return report<\/p>\n<p># print(generate_report())<\/p>\n<h4>\u7ed3\u5408\u65e5\u5fd7\u5206\u6790\u957f\u671f\u8d8b\u52bf<\/h4>\n<p>import logging<br \/>\nfrom logging.handlers import TimedRotatingFileHandler<\/p>\n<p>def setup_monitoring_logger():<br \/>\n    &#034;&#034;&#034;\u8bbe\u7f6e\u76d1\u63a7\u65e5\u5fd7&#034;&#034;&#034;<br \/>\n    logger &#061; logging.getLogger(&#039;system_monitor&#039;)<br \/>\n    logger.setLevel(logging.INFO)<\/p>\n<p>    # \u6bcf\u5929\u8f6e\u6362\u65e5\u5fd7&#xff0c;\u4fdd\u75597\u5929<br \/>\n    handler &#061; TimedRotatingFileHandler(<br \/>\n        &#039;system_monitor.log&#039;,<br \/>\n        when&#061;&#039;D&#039;,<br \/>\n        interval&#061;1,<br \/>\n        backupCount&#061;7<br \/>\n    )<br \/>\n    formatter &#061; logging.Formatter(&#039;%(asctime)s &#8211; %(message)s&#039;)<br \/>\n    handler.setFormatter(formatter)<br \/>\n    logger.addHandler(handler)<\/p>\n<p>    return logger<\/p>\n<p>def log_system_stats(logger, interval&#061;300):<br \/>\n    &#034;&#034;&#034;\u5b9a\u671f\u8bb0\u5f55\u7cfb\u7edf\u72b6\u6001\u5230\u65e5\u5fd7&#034;&#034;&#034;<br \/>\n    while True:<br \/>\n        stats &#061; {<br \/>\n            &#039;cpu&#039;: psutil.cpu_percent(interval&#061;1),<br \/>\n            &#039;mem&#039;: psutil.virtual_memory().percent,<br \/>\n            &#039;disk&#039;: psutil.disk_usage(&#039;\/&#039;).percent<br \/>\n        }<br \/>\n        logger.info(f&#034;CPU: {stats[&#039;cpu&#039;]}% | MEM: {stats[&#039;mem&#039;]}% | DISK: {stats[&#039;disk&#039;]}%&#034;)<br \/>\n        time.sleep(interval)<\/p>\n<p># logger &#061; setup_monitoring_logger()<br \/>\n# log_system_stats(logger)<\/p>\n<h3>\u9ad8\u7ea7\u5e94\u7528\u4e0e\u6280\u5de7<\/h3>\n<h4>\u591a\u5e73\u53f0\u9002\u914d\u7684\u6ce8\u610f\u4e8b\u9879<\/h4>\n<p>\u5728\u4f7f\u7528psutil\u8fdb\u884c\u8de8\u5e73\u53f0\u5f00\u53d1\u65f6&#xff0c;\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u5dee\u5f02&#xff1a;<\/p>\n<li>\n<p>\u8def\u5f84\u8868\u793a&#xff1a;<\/p>\n<ul>\n<li>Windows\u4f7f\u7528\u53cd\u659c\u6760(\\\\)&#xff0c;\u800cUnix-like\u7cfb\u7edf\u4f7f\u7528\u6b63\u659c\u6760(\/)<\/li>\n<li>\u5efa\u8bae\u4f7f\u7528os.path\u6a21\u5757\u5904\u7406\u8def\u5f84<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u8fdb\u7a0b\u5c5e\u6027\u5dee\u5f02&#xff1a;<\/p>\n<p>def get_process_info(pid):<br \/>\n    try:<br \/>\n        p &#061; psutil.Process(pid)<br \/>\n        info &#061; {<br \/>\n            &#039;pid&#039;: pid,<br \/>\n            &#039;name&#039;: p.name(),<br \/>\n            &#039;status&#039;: p.status(),<br \/>\n            &#039;cpu&#039;: p.cpu_percent(interval&#061;0.1),<br \/>\n            &#039;memory&#039;: p.memory_info().rss<br \/>\n        }<\/p>\n<p>        # Windows\u7279\u6709\u5c5e\u6027<br \/>\n        if hasattr(p, &#039;username&#039;):<br \/>\n            info[&#039;user&#039;] &#061; p.username()<\/p>\n<p>        # Unix\u7279\u6709\u5c5e\u6027<br \/>\n        if hasattr(p, &#039;terminal&#039;):<br \/>\n            info[&#039;terminal&#039;] &#061; p.terminal()<\/p>\n<p>        return info<br \/>\n    except psutil.NoSuchProcess:<br \/>\n        return None<\/p>\n<\/li>\n<li>\n<p>\u7f51\u7edc\u63a5\u53e3\u547d\u540d&#xff1a;<\/p>\n<ul>\n<li>Windows: &#034;\u4ee5\u592a\u7f51&#034;\u3001&#034;Wi-Fi&#034;<\/li>\n<li>Linux: &#034;eth0&#034;\u3001&#034;wlan0&#034;<\/li>\n<li>macOS: &#034;en0&#034;\u3001&#034;en1&#034;<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u78c1\u76d8\u5206\u533a\u8868\u793a&#xff1a;<\/p>\n<ul>\n<li>Windows: &#034;C:&#034;\u3001&#034;D:&#034;<\/li>\n<li>Unix-like: &#034;\/&#034;, &#034;\/home&#034;<\/li>\n<\/ul>\n<\/li>\n<h4>\u7ed3\u5408\u5176\u4ed6\u5de5\u5177\u8fdb\u884c\u6570\u636e\u53ef\u89c6\u5316<\/h4>\n<h5>\u4f7f\u7528Pandas\u5206\u6790\u6570\u636e<\/h5>\n<p>import pandas as pd<\/p>\n<p>def collect_system_data(samples&#061;10, interval&#061;1):<br \/>\n    &#034;&#034;&#034;\u6536\u96c6\u7cfb\u7edf\u6570\u636e\u5e76\u8fd4\u56deDataFrame&#034;&#034;&#034;<br \/>\n    data &#061; []<br \/>\n    for _ in range(samples):<br \/>\n        record &#061; {<br \/>\n            &#039;timestamp&#039;: pd.Timestamp.now(),<br \/>\n            &#039;cpu&#039;: psutil.cpu_percent(interval&#061;interval),<br \/>\n            &#039;memory&#039;: psutil.virtual_memory().percent,<br \/>\n            &#039;disk&#039;: psutil.disk_usage(&#039;\/&#039;).percent<br \/>\n        }<br \/>\n        data.append(record)<br \/>\n        time.sleep(interval)<\/p>\n<p>    return pd.DataFrame(data)<\/p>\n<p># df &#061; collect_system_data()<br \/>\n# print(df.describe())<\/p>\n<h5>\u4f7f\u7528Matplotlib\u7ed8\u5236\u56fe\u8868<\/h5>\n<p>import matplotlib.pyplot as plt<\/p>\n<p>def plot_system_metrics(df):<br \/>\n    &#034;&#034;&#034;\u7ed8\u5236\u7cfb\u7edf\u6307\u6807\u56fe\u8868&#034;&#034;&#034;<br \/>\n    fig, axes &#061; plt.subplots(nrows&#061;3, ncols&#061;1, figsize&#061;(10, 8))<\/p>\n<p>    # CPU\u4f7f\u7528\u7387<br \/>\n    df.plot(x&#061;&#039;timestamp&#039;, y&#061;&#039;cpu&#039;, ax&#061;axes[0], title&#061;&#039;CPU Usage (%)&#039;, legend&#061;False)<br \/>\n    axes[0].set_ylim(0, 100)<\/p>\n<p>    # \u5185\u5b58\u4f7f\u7528\u7387<br \/>\n    df.plot(x&#061;&#039;timestamp&#039;, y&#061;&#039;memory&#039;, ax&#061;axes[1], title&#061;&#039;Memory Usage (%)&#039;, legend&#061;False)<br \/>\n    axes[1].set_ylim(0, 100)<\/p>\n<p>    # \u78c1\u76d8\u4f7f\u7528\u7387<br \/>\n    df.plot(x&#061;&#039;timestamp&#039;, y&#061;&#039;disk&#039;, ax&#061;axes[2], title&#061;&#039;Disk Usage (%)&#039;, legend&#061;False)<br \/>\n    axes[2].set_ylim(0, 100)<\/p>\n<p>    plt.tight_layout()<br \/>\n    plt.show()<\/p>\n<p># plot_system_metrics(df)<\/p>\n<h4>\u5f02\u5e38\u5904\u7406\u4e0e\u6027\u80fd\u8c03\u4f18<\/h4>\n<h5>\u5065\u58ee\u7684\u8fdb\u7a0b\u76d1\u63a7<\/h5>\n<p>def safe_process_info(pid):<br \/>\n    &#034;&#034;&#034;\u5b89\u5168\u83b7\u53d6\u8fdb\u7a0b\u4fe1\u606f&#xff0c;\u5904\u7406\u5404\u79cd\u5f02\u5e38&#034;&#034;&#034;<br \/>\n    try:<br \/>\n        p &#061; psutil.Process(pid)<br \/>\n        return {<br \/>\n            &#039;pid&#039;: pid,<br \/>\n            &#039;name&#039;: p.name(),<br \/>\n            &#039;status&#039;: p.status(),<br \/>\n            &#039;cpu&#039;: p.cpu_percent(interval&#061;0.1),<br \/>\n            &#039;memory&#039;: p.memory_percent(),<br \/>\n            &#039;threads&#039;: p.num_threads()<br \/>\n        }<br \/>\n    except psutil.NoSuchProcess:<br \/>\n        return {&#039;error&#039;: &#039;Process not found&#039;}<br \/>\n    except psutil.AccessDenied:<br \/>\n        return {&#039;error&#039;: &#039;Access denied&#039;}<br \/>\n    except psutil.ZombieProcess:<br \/>\n        return {&#039;error&#039;: &#039;Zombie process&#039;}<br \/>\n    except Exception as e:<br \/>\n        return {&#039;error&#039;: str(e)}<\/p>\n<h5>\u6027\u80fd\u4f18\u5316\u6280\u5de7<\/h5>\n<li>\n<p>\u6279\u91cf\u83b7\u53d6\u5c5e\u6027&#xff1a;<\/p>\n<p># \u4f4e\u6548\u65b9\u5f0f<br \/>\ncpu &#061; p.cpu_percent()<br \/>\nmem &#061; p.memory_percent()<\/p>\n<p># \u9ad8\u6548\u65b9\u5f0f<br \/>\ninfo &#061; p.as_dict(attrs&#061;[&#039;cpu_percent&#039;, &#039;memory_percent&#039;])<\/p>\n<\/li>\n<li>\n<p>\u51cf\u5c11\u8fdb\u7a0b\u5217\u8868\u626b\u63cf\u9891\u7387&#xff1a;<\/p>\n<p># \u7f13\u5b58\u8fdb\u7a0b\u5217\u8868<br \/>\ncached_pids &#061; set(psutil.pids())<\/p>\n<p># \u5b9a\u671f\u66f4\u65b0\u7f13\u5b58<br \/>\nif time.time() &#8211; last_update &gt; 60:<br \/>\n    cached_pids &#061; set(psutil.pids())<br \/>\n    last_update &#061; time.time()<\/p>\n<\/li>\n<li>\n<p>\u4f7f\u7528with\u8bed\u53e5\u7ba1\u7406\u8d44\u6e90&#xff1a;<\/p>\n<p>with psutil.Process(pid) as p:<br \/>\n    print(p.name())<br \/>\n    print(p.status())<\/p>\n<\/li>\n<h3>\u5b9e\u9645\u6848\u4f8b<\/h3>\n<h4>\u670d\u52a1\u5668\u76d1\u63a7\u811a\u672c\u7684\u5b9e\u73b0<\/h4>\n<p>#!\/usr\/bin\/env python3<br \/>\n&#034;&#034;&#034;<br \/>\n\u670d\u52a1\u5668\u8d44\u6e90\u76d1\u63a7\u811a\u672c<br \/>\n\u76d1\u63a7CPU\u3001\u5185\u5b58\u3001\u78c1\u76d8\u3001\u7f51\u7edc\u4f7f\u7528\u60c5\u51b5<br \/>\n\u5f53\u8d44\u6e90\u8d85\u8fc7\u9608\u503c\u65f6\u53d1\u9001\u544a\u8b66<br \/>\n&#034;&#034;&#034;<\/p>\n<p>import psutil<br \/>\nimport time<br \/>\nimport smtplib<br \/>\nfrom email.mime.text import MIMEText<\/p>\n<p># \u914d\u7f6e\u53c2\u6570<br \/>\nTHRESHOLDS &#061; {<br \/>\n    &#039;cpu&#039;: 90,       # CPU\u4f7f\u7528\u7387\u9608\u503c(%)<br \/>\n    &#039;memory&#039;: 90,    # \u5185\u5b58\u4f7f\u7528\u7387\u9608\u503c(%)<br \/>\n    &#039;disk&#039;: 90,      # \u78c1\u76d8\u4f7f\u7528\u7387\u9608\u503c(%)<br \/>\n}<\/p>\n<p>ALERT_INTERVAL &#061; 3600  # \u544a\u8b66\u95f4\u9694(\u79d2)<br \/>\nMONITOR_INTERVAL &#061; 5   # \u76d1\u63a7\u95f4\u9694(\u79d2)<\/p>\n<p># \u90ae\u4ef6\u914d\u7f6e<br \/>\nSMTP_SERVER &#061; &#039;smtp.example.com&#039;<br \/>\nSMTP_PORT &#061; 587<br \/>\nEMAIL_USER &#061; &#039;monitor&#064;example.com&#039;<br \/>\nEMAIL_PASS &#061; &#039;password&#039;<br \/>\nEMAIL_TO &#061; &#039;admin&#064;example.com&#039;<\/p>\n<p>last_alert_time &#061; 0<\/p>\n<p>def check_thresholds():<br \/>\n    &#034;&#034;&#034;\u68c0\u67e5\u5404\u9879\u6307\u6807\u662f\u5426\u8d85\u8fc7\u9608\u503c&#034;&#034;&#034;<br \/>\n    alerts &#061; []<\/p>\n<p>    # CPU\u68c0\u67e5<br \/>\n    cpu_percent &#061; psutil.cpu_percent(interval&#061;1)<br \/>\n    if cpu_percent &gt; THRESHOLDS[&#039;cpu&#039;]:<br \/>\n        alerts.append(f&#034;CPU\u4f7f\u7528\u7387\u8fc7\u9ad8: {cpu_percent}%&#034;)<\/p>\n<p>    # \u5185\u5b58\u68c0\u67e5<br \/>\n    mem_percent &#061; psutil.virtual_memory().percent<br \/>\n    if mem_percent &gt; THRESHOLDS[&#039;memory&#039;]:<br \/>\n        alerts.append(f&#034;\u5185\u5b58\u4f7f\u7528\u7387\u8fc7\u9ad8: {mem_percent}%&#034;)<\/p>\n<p>    # \u78c1\u76d8\u68c0\u67e5<br \/>\n    disk_percent &#061; psutil.disk_usage(&#039;\/&#039;).percent<br \/>\n    if disk_percent &gt; THRESHOLDS[&#039;disk&#039;]:<br \/>\n        alerts.append(f&#034;\u78c1\u76d8\u4f7f\u7528\u7387\u8fc7\u9ad8: {disk_percent}%&#034;)<\/p>\n<p>    return alerts<\/p>\n<p>def send_alert(alerts):<br \/>\n    &#034;&#034;&#034;\u53d1\u9001\u544a\u8b66\u90ae\u4ef6&#034;&#034;&#034;<br \/>\n    global last_alert_time<\/p>\n<p>    current_time &#061; time.time()<br \/>\n    if current_time &#8211; last_alert_time &lt; ALERT_INTERVAL:<br \/>\n        return<\/p>\n<p>    subject &#061; &#034;\u670d\u52a1\u5668\u8d44\u6e90\u544a\u8b66&#034;<br \/>\n    body &#061; &#034;\\\\n&#034;.join(alerts)<\/p>\n<p>    msg &#061; MIMEText(body)<br \/>\n    msg[&#039;Subject&#039;] &#061; subject<br \/>\n    msg[&#039;From&#039;] &#061; EMAIL_USER<br \/>\n    msg[&#039;To&#039;] &#061; EMAIL_TO<\/p>\n<p>    try:<br \/>\n        with smtplib.SMTP(SMTP_SERVER, SMTP_PORT) as server:<br \/>\n            server.starttls()<br \/>\n            server.login(EMAIL_USER, EMAIL_PASS)<br \/>\n            server.send_message(msg)<br \/>\n        last_alert_time &#061; current_time<br \/>\n        print(f&#034;\u544a\u8b66\u5df2\u53d1\u9001: {alerts}&#034;)<br \/>\n    except Exception as e:<br \/>\n        print(f&#034;\u53d1\u9001\u544a\u8b66\u5931\u8d25: {e}&#034;)<\/p>\n<p>def main():<br \/>\n    &#034;&#034;&#034;\u4e3b\u76d1\u63a7\u5faa\u73af&#034;&#034;&#034;<br \/>\n    print(&#034;\u542f\u52a8\u670d\u52a1\u5668\u76d1\u63a7&#8230;&#034;)<br \/>\n    while True:<br \/>\n        alerts &#061; check_thresholds()<br \/>\n        if alerts:<br \/>\n            send_alert(alerts)<br \/>\n        time.sleep(MONITOR_INTERVAL)<\/p>\n<p>if __name__ &#061;&#061; &#039;__main__&#039;:<br \/>\n    main()<\/p>\n<h4>\u81ea\u52a8\u5316\u8fd0\u7ef4\u4efb\u52a1\u793a\u4f8b<\/h4>\n<p>#!\/usr\/bin\/env python3<br \/>\n&#034;&#034;&#034;<br \/>\n\u81ea\u52a8\u5316\u8fd0\u7ef4\u811a\u672c<br \/>\n1. \u76d1\u63a7\u6307\u5b9a\u8fdb\u7a0b&#xff0c;\u5982\u679c\u5d29\u6e83\u5219\u81ea\u52a8\u91cd\u542f<br \/>\n2. \u6e05\u7406\u8fc7\u671f\u7684\u65e5\u5fd7\u6587\u4ef6<br \/>\n3. \u5b9a\u671f\u5907\u4efd\u91cd\u8981\u6570\u636e<br \/>\n&#034;&#034;&#034;<\/p>\n<p>import psutil<br \/>\nimport os<br \/>\nimport time<br \/>\nimport shutil<br \/>\nimport subprocess<br \/>\nfrom datetime import datetime, timedelta<\/p>\n<p># \u914d\u7f6e\u53c2\u6570<br \/>\nPROCESS_NAME &#061; &#034;my_service&#034;    # \u8981\u76d1\u63a7\u7684\u8fdb\u7a0b\u540d<br \/>\nPROCESS_CMD &#061; [&#034;python&#034;, &#034;service.py&#034;]  # \u8fdb\u7a0b\u542f\u52a8\u547d\u4ee4<br \/>\nLOG_DIR &#061; &#034;\/var\/log\/myapp&#034;     # \u65e5\u5fd7\u76ee\u5f55<br \/>\nBACKUP_DIR &#061; &#034;\/backups&#034;        # \u5907\u4efd\u76ee\u5f55<br \/>\nDATA_DIR &#061; &#034;\/data&#034;             # \u8981\u5907\u4efd\u7684\u6570\u636e\u76ee\u5f55<br \/>\nRETENTION_DAYS &#061; 7             # \u65e5\u5fd7\u548c\u5907\u4efd\u4fdd\u7559\u5929\u6570<br \/>\nCHECK_INTERVAL &#061; 60            # \u68c0\u67e5\u95f4\u9694(\u79d2)<\/p>\n<p>def find_process():<br \/>\n    &#034;&#034;&#034;\u67e5\u627e\u6307\u5b9a\u8fdb\u7a0b&#034;&#034;&#034;<br \/>\n    for proc in psutil.process_iter([&#039;name&#039;, &#039;cmdline&#039;]):<br \/>\n        try:<br \/>\n            if proc.info[&#039;name&#039;] &#061;&#061; PROCESS_NAME or \\\\<br \/>\n               (proc.info[&#039;cmdline&#039;] and proc.info[&#039;cmdline&#039;][0] &#061;&#061; PROCESS_CMD[0]):<br \/>\n                return proc<br \/>\n        except (psutil.NoSuchProcess, psutil.AccessDenied):<br \/>\n            continue<br \/>\n    return None<\/p>\n<p>def start_process():<br \/>\n    &#034;&#034;&#034;\u542f\u52a8\u8fdb\u7a0b&#034;&#034;&#034;<br \/>\n    print(f&#034;\u542f\u52a8\u8fdb\u7a0b: {&#039; &#039;.join(PROCESS_CMD)}&#034;)<br \/>\n    subprocess.Popen(PROCESS_CMD)<\/p>\n<p>def cleanup_files(directory, pattern, days):<br \/>\n    &#034;&#034;&#034;\u6e05\u7406\u8fc7\u671f\u6587\u4ef6&#034;&#034;&#034;<br \/>\n    now &#061; datetime.now()<br \/>\n    cutoff &#061; now &#8211; timedelta(days&#061;days)<\/p>\n<p>    for filename in os.listdir(directory):<br \/>\n        if filename.endswith(pattern):<br \/>\n            filepath &#061; os.path.join(directory, filename)<br \/>\n            mtime &#061; datetime.fromtimestamp(os.path.getmtime(filepath))<br \/>\n            if mtime &lt; cutoff:<br \/>\n                print(f&#034;\u5220\u9664\u8fc7\u671f\u6587\u4ef6: {filepath}&#034;)<br \/>\n                os.remove(filepath)<\/p>\n<p>def create_backup():<br \/>\n    &#034;&#034;&#034;\u521b\u5efa\u6570\u636e\u5907\u4efd&#034;&#034;&#034;<br \/>\n    timestamp &#061; datetime.now().strftime(&#034;%Y%m%d_%H%M%S&#034;)<br \/>\n    backup_name &#061; f&#034;backup_{timestamp}.tar.gz&#034;<br \/>\n    backup_path &#061; os.path.join(BACKUP_DIR, backup_name)<\/p>\n<p>    print(f&#034;\u521b\u5efa\u5907\u4efd: {backup_path}&#034;)<br \/>\n    shutil.make_archive(backup_path.replace(&#039;.tar.gz&#039;, &#039;&#039;), &#039;gztar&#039;, DATA_DIR)<br \/>\n    return backup_path<\/p>\n<p>def main():<br \/>\n    &#034;&#034;&#034;\u4e3b\u5faa\u73af&#034;&#034;&#034;<br \/>\n    print(&#034;\u542f\u52a8\u81ea\u52a8\u5316\u8fd0\u7ef4\u811a\u672c&#8230;&#034;)<\/p>\n<p>    while True:<br \/>\n        # \u68c0\u67e5\u8fdb\u7a0b<br \/>\n        proc &#061; find_process()<br \/>\n        if proc is None:<br \/>\n            print(&#034;\u8fdb\u7a0b\u672a\u8fd0\u884c&#xff0c;\u5c1d\u8bd5\u542f\u52a8&#8230;&#034;)<br \/>\n            start_process()<br \/>\n        else:<br \/>\n            print(f&#034;\u8fdb\u7a0b\u8fd0\u884c\u4e2d&#xff0c;PID: {proc.pid}&#034;)<\/p>\n<p>        # \u6e05\u7406\u8fc7\u671f\u65e5\u5fd7<br \/>\n        print(&#034;\u6e05\u7406\u8fc7\u671f\u65e5\u5fd7&#8230;&#034;)<br \/>\n        cleanup_files(LOG_DIR, &#034;.log&#034;, RETENTION_DAYS)<\/p>\n<p>        # \u521b\u5efa\u5b9a\u671f\u5907\u4efd<br \/>\n        if datetime.now().hour &#061;&#061; 2:  # \u6bcf\u5929\u51cc\u66682\u70b9\u5907\u4efd<br \/>\n            print(&#034;\u6267\u884c\u6bcf\u65e5\u5907\u4efd&#8230;&#034;)<br \/>\n            create_backup()<br \/>\n            print(&#034;\u6e05\u7406\u8fc7\u671f\u5907\u4efd&#8230;&#034;)<br \/>\n            cleanup_files(BACKUP_DIR, &#034;.tar.gz&#034;, RETENTION_DAYS)<\/p>\n<p>        time.sleep(CHECK_INTERVAL)<\/p>\n<p>if __name__ &#061;&#061; &#039;__main__&#039;:<br \/>\n    main()<\/p>\n<h3>\u5e38\u89c1\u95ee\u9898\u4e0e\u89e3\u51b3\u65b9\u6848<\/h3>\n<h4>\u5e38\u89c1\u9519\u8bef\u4e0e\u8c03\u8bd5\u65b9\u6cd5<\/h4>\n<li>\n<p>NoSuchProcess\u9519\u8bef&#xff1a;<\/p>\n<ul>\n<li>\u95ee\u9898&#xff1a;\u5c1d\u8bd5\u8bbf\u95ee\u5df2\u7ec8\u6b62\u7684\u8fdb\u7a0b\u65f6\u629b\u51fa<\/li>\n<li>\u89e3\u51b3\u65b9\u6848&#xff1a;\n<p>try:<br \/>\n    p &#061; psutil.Process(pid)<br \/>\n    print(p.name())<br \/>\nexcept psutil.NoSuchProcess:<br \/>\n    print(f&#034;\u8fdb\u7a0b {pid} \u5df2\u7ec8\u6b62&#034;)<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>AccessDenied\u9519\u8bef&#xff1a;<\/p>\n<ul>\n<li>\u95ee\u9898&#xff1a;\u6743\u9650\u4e0d\u8db3\u8bbf\u95ee\u67d0\u4e9b\u8fdb\u7a0b\u4fe1\u606f<\/li>\n<li>\u89e3\u51b3\u65b9\u6848&#xff1a;\n<ul>\n<li>Linux\/Mac: \u4f7f\u7528sudo\u8fd0\u884c\u811a\u672c<\/li>\n<li>Windows: \u4ee5\u7ba1\u7406\u5458\u8eab\u4efd\u8fd0\u884c<\/li>\n<li>\u6216\u8005\u6355\u83b7\u5f02\u5e38\u5904\u7406&#xff1a;\n<p>try:<br \/>\n    p &#061; psutil.Process(pid)<br \/>\n    print(p.environ())<br \/>\nexcept psutil.AccessDenied:<br \/>\n    print(&#034;\u6743\u9650\u4e0d\u8db3&#xff0c;\u65e0\u6cd5\u8bbf\u95ee\u8fdb\u7a0b\u73af\u5883&#034;)<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>ZombieProcess\u9519\u8bef&#xff1a;<\/p>\n<ul>\n<li>\u95ee\u9898&#xff1a;\u8fdb\u7a0b\u5904\u4e8e\u50f5\u5c38\u72b6\u6001<\/li>\n<li>\u89e3\u51b3\u65b9\u6848&#xff1a;\n<p>try:<br \/>\n    p &#061; psutil.Process(pid)<br \/>\n    if p.status() &#061;&#061; psutil.STATUS_ZOMBIE:<br \/>\n        print(&#034;\u8fdb\u7a0b\u5904\u4e8e\u50f5\u5c38\u72b6\u6001&#034;)<br \/>\nexcept psutil.ZombieProcess:<br \/>\n    print(&#034;\u65e0\u6cd5\u8bbf\u95ee\u50f5\u5c38\u8fdb\u7a0b&#034;)<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u6027\u80fd\u95ee\u9898&#xff1a;<\/p>\n<ul>\n<li>\u95ee\u9898&#xff1a;\u9891\u7e41\u83b7\u53d6\u8fdb\u7a0b\u5217\u8868\u5bfc\u81f4\u6027\u80fd\u4e0b\u964d<\/li>\n<li>\u89e3\u51b3\u65b9\u6848&#xff1a;\n<ul>\n<li>\u51cf\u5c11\u626b\u63cf\u9891\u7387<\/li>\n<li>\u7f13\u5b58\u8fdb\u7a0b\u5217\u8868<\/li>\n<li>\u4f7f\u7528pids()\u4ee3\u66ffprocess_iter()\u83b7\u53d6\u7279\u5b9aPID<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<h4>\u6027\u80fd\u74f6\u9888\u5206\u6790<\/h4>\n<li>\n<p>\u8bc6\u522b\u9ad8CPU\u8fdb\u7a0b&#xff1a;<\/p>\n<p>def find_top_cpu_processes(n&#061;5):<br \/>\n    &#034;&#034;&#034;\u67e5\u627eCPU\u5360\u7528\u6700\u9ad8\u7684n\u4e2a\u8fdb\u7a0b&#034;&#034;&#034;<br \/>\n    procs &#061; []<br \/>\n    for p in psutil.process_iter([&#039;pid&#039;, &#039;name&#039;, &#039;cpu_percent&#039;]):<br \/>\n        try:<br \/>\n            p.info[&#039;cpu_percent&#039;] &#061; p.info[&#039;cpu_percent&#039;] or 0<br \/>\n            procs.append(p.info)<br \/>\n        except (psutil.NoSuchProcess, psutil.AccessDenied):<br \/>\n            continue<\/p>\n<p>    # \u6309CPU\u4f7f\u7528\u7387\u6392\u5e8f<br \/>\n    return sorted(procs, key&#061;lambda x: x[&#039;cpu_percent&#039;], reverse&#061;True)[:n]<\/p>\n<\/li>\n<li>\n<p>\u8bc6\u522b\u5185\u5b58\u6cc4\u6f0f&#xff1a;<\/p>\n<p>def monitor_memory_leak(pid, interval&#061;60, threshold&#061;10):<br \/>\n    &#034;&#034;&#034;\u76d1\u63a7\u5185\u5b58\u6cc4\u6f0f&#034;&#034;&#034;<br \/>\n    baseline &#061; None<br \/>\n    while True:<br \/>\n        try:<br \/>\n            p &#061; psutil.Process(pid)<br \/>\n            mem &#061; p.memory_info().rss \/ 1024 \/ 1024  # MB<\/p>\n<p>            if baseline is None:<br \/>\n                baseline &#061; mem<br \/>\n                print(f&#034;\u521d\u59cb\u5185\u5b58: {baseline:.2f} MB&#034;)<br \/>\n            else:<br \/>\n                increase &#061; mem &#8211; baseline<br \/>\n                if increase &gt; threshold:<br \/>\n                    print(f&#034;\u8b66\u544a: \u5185\u5b58\u589e\u52a0\u4e86 {increase:.2f} MB (\u5f53\u524d: {mem:.2f} MB)&#034;)<\/p>\n<p>            time.sleep(interval)<br \/>\n        except psutil.NoSuchProcess:<br \/>\n            print(&#034;\u8fdb\u7a0b\u5df2\u7ec8\u6b62&#034;)<br \/>\n            break<\/p>\n<\/li>\n<li>\n<p>\u78c1\u76d8IO\u74f6\u9888\u5206\u6790&#xff1a;<\/p>\n<p>def analyze_disk_io():<br \/>\n    &#034;&#034;&#034;\u5206\u6790\u78c1\u76d8IO\u74f6\u9888&#034;&#034;&#034;<br \/>\n    # \u83b7\u53d6\u78c1\u76d8IO\u7edf\u8ba1<br \/>\n    disk_io &#061; psutil.disk_io_counters()<br \/>\n    print(f&#034;\u8bfb\u53d6\u6b21\u6570: {disk_io.read_count}&#034;)<br \/>\n    print(f&#034;\u5199\u5165\u6b21\u6570: {disk_io.write_count}&#034;)<\/p>\n<p>    # \u67e5\u627e\u9ad8IO\u8fdb\u7a0b<br \/>\n    high_io_procs &#061; []<br \/>\n    for p in psutil.process_iter([&#039;pid&#039;, &#039;name&#039;, &#039;io_counters&#039;]):<br \/>\n        try:<br \/>\n            io &#061; p.info[&#039;io_counters&#039;]<br \/>\n            if io and (io.read_bytes &gt; 100*1024*1024 or io.write_bytes &gt; 100*1024*1024):  # 100MB<br \/>\n                high_io_procs.append({<br \/>\n                    &#039;pid&#039;: p.info[&#039;pid&#039;],<br \/>\n                    &#039;name&#039;: p.info[&#039;name&#039;],<br \/>\n                    &#039;read&#039;: io.read_bytes \/ 1024 \/ 1024,<br \/>\n                    &#039;write&#039;: io.write_bytes \/ 1024 \/ 1024<br \/>\n                })<br \/>\n        except (psutil.NoSuchProcess, psutil.AccessDenied):<br \/>\n            continue<\/p>\n<p>    return high_io_procs<\/p>\n<\/li>\n<h3>\u603b\u7ed3<\/h3>\n<h4>psutil\u7684\u4f18\u52bf\u4e0e\u9002\u7528\u573a\u666f<\/h4>\n<p>psutil\u4f5c\u4e3aPython\u7cfb\u7edf\u76d1\u63a7\u7684\u6807\u51c6\u5e93&#xff0c;\u5177\u6709\u4ee5\u4e0b\u663e\u8457\u4f18\u52bf&#xff1a;<\/p>\n<li>\u8de8\u5e73\u53f0\u652f\u6301&#xff1a;\u7edf\u4e00\u7684API\u5728\u4e0d\u540c\u7684\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u5de5\u4f5c<\/li>\n<li>\u529f\u80fd\u5168\u9762&#xff1a;\u8986\u76d6CPU\u3001\u5185\u5b58\u3001\u78c1\u76d8\u3001\u7f51\u7edc\u3001\u8fdb\u7a0b\u7b49\u5404\u4e2a\u65b9\u9762<\/li>\n<li>\u7b80\u5355\u6613\u7528&#xff1a;\u76f4\u89c2\u7684API\u8bbe\u8ba1&#xff0c;\u5b66\u4e60\u66f2\u7ebf\u5e73\u7f13<\/li>\n<li>\u6027\u80fd\u9ad8\u6548&#xff1a;\u5e95\u5c42\u4f7f\u7528C\u6269\u5c55\u5b9e\u73b0&#xff0c;\u8d44\u6e90\u6d88\u8017\u4f4e<\/li>\n<li>\u6d3b\u8dc3\u7ef4\u62a4&#xff1a;\u6301\u7eed\u66f4\u65b0&#xff0c;\u652f\u6301\u6700\u65b0\u7684Python\u7248\u672c\u548c\u64cd\u4f5c\u7cfb\u7edf<\/li>\n<p>psutil\u975e\u5e38\u9002\u5408\u4ee5\u4e0b\u573a\u666f&#xff1a;<\/p>\n<ul>\n<li>\u670d\u52a1\u5668\u76d1\u63a7\u548c\u544a\u8b66\u7cfb\u7edf<\/li>\n<li>\u81ea\u52a8\u5316\u8fd0\u7ef4\u5de5\u5177\u5f00\u53d1<\/li>\n<li>\u7cfb\u7edf\u6027\u80fd\u5206\u6790\u548c\u8c03\u4f18<\/li>\n<li>\u8d44\u6e90\u4f7f\u7528\u7edf\u8ba1\u548c\u62a5\u544a\u751f\u6210<\/li>\n<li>\u8fdb\u7a0b\u7ba1\u7406\u548c\u76d1\u63a7\u5de5\u5177<\/li>\n<\/ul>\n<h4>\u672a\u6765\u53d1\u5c55\u65b9\u5411\u4e0e\u793e\u533a\u652f\u6301<\/h4>\n<p>psutil\u9879\u76ee\u6301\u7eed\u6d3b\u8dc3\u53d1\u5c55&#xff0c;\u672a\u6765\u53ef\u80fd\u7684\u65b9\u5411\u5305\u62ec&#xff1a;<\/p>\n<li>\u66f4\u4e30\u5bcc\u7684\u5bb9\u5668\u652f\u6301&#xff1a;\u589e\u5f3a\u5bf9Docker\u3001Kubernetes\u7b49\u5bb9\u5668\u73af\u5883\u7684\u76d1\u63a7<\/li>\n<li>GPU\u76d1\u63a7&#xff1a;\u589e\u52a0\u5bf9GPU\u4f7f\u7528\u60c5\u51b5\u7684\u76d1\u63a7\u529f\u80fd<\/li>\n<li>\u66f4\u8be6\u7ec6\u7684\u8fdb\u7a0b\u5206\u6790&#xff1a;\u63d0\u4f9b\u66f4\u6df1\u5c42\u6b21\u7684\u8fdb\u7a0b\u5256\u6790\u529f\u80fd<\/li>\n<li>\u5f02\u6b65API\u652f\u6301&#xff1a;\u9002\u5e94\u73b0\u4ee3\u5f02\u6b65\u7f16\u7a0b\u8303\u5f0f<\/li>\n<p>psutil\u62e5\u6709\u4e00\u4e2a\u6d3b\u8dc3\u7684\u5f00\u6e90\u793e\u533a&#xff0c;\u7528\u6237\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u83b7\u53d6\u652f\u6301\u548c\u53c2\u4e0e\u8d21\u732e&#xff1a;<\/p>\n<ul>\n<li>GitHub\u4ed3\u5e93: https:\/\/github.com\/giampaolo\/psutil<\/li>\n<li>\u5b98\u65b9\u6587\u6863: https:\/\/psutil.readthedocs.io\/<\/li>\n<li>\u90ae\u4ef6\u5217\u8868\u548c\u95ee\u9898\u8ffd\u8e2a<\/li>\n<\/ul>\n<p>\u901a\u8fc7psutil&#xff0c;Python\u5f00\u53d1\u8005\u53ef\u4ee5\u8f7b\u677e\u6784\u5efa\u5f3a\u5927\u7684\u7cfb\u7edf\u76d1\u63a7\u548c\u7ba1\u7406\u5de5\u5177&#xff0c;\u6781\u5927\u5730\u7b80\u5316\u4e86\u4e0e\u7cfb\u7edf\u8d44\u6e90\u4ea4\u4e92\u7684\u590d\u6742\u6027&#xff0c;\u662f\u6bcf\u4e2a\u7cfb\u7edf\u7ba1\u7406\u5458\u548cDevOps\u5de5\u7a0b\u5e08\u5de5\u5177\u7bb1\u4e2d\u4e0d\u53ef\u6216\u7f3a\u7684\u5229\u5668\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb661\u6b21\uff0c\u70b9\u8d5e30\u6b21\uff0c\u6536\u85cf7\u6b21\u3002psutil\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u8de8\u5e73\u53f0Python\u5e93\uff0c\u7528\u4e8e\u76d1\u63a7\u7cfb\u7edf\u8d44\u6e90\u548c\u8fdb\u7a0b\u7ba1\u7406\u3002\u5b83\u652f\u6301\u83b7\u53d6CPU\u3001\u5185\u5b58\u3001\u78c1\u76d8\u3001\u7f51\u7edc\u7b49\u7cfb\u7edf\u4fe1\u606f\uff0c\u5e76\u80fd\u8fdb\u884c\u8be6\u7ec6\u7684\u8fdb\u7a0b\u63a7\u5236\u3002psutil\u7684\u4e3b\u8981\u4f18\u52bf\u5305\u62ec\u7edf\u4e00\u7684\u8de8\u5e73\u53f0API\u3001\u5168\u9762\u7684\u76d1\u63a7\u529f\u80fd\u3001\u9ad8\u6548\u6027\u80fd\u548c\u6d3b\u8dc3\u7684\u793e\u533a\u652f\u6301\u3002\u8be5\u5e93\u9002\u7528\u4e8e\u670d\u52a1\u5668\u76d1\u63a7\u3001\u81ea\u52a8\u5316\u8fd0\u7ef4\u3001\u6027\u80fd\u5206\u6790\u7b49\u573a\u666f\uff0c\u53ef\u901a\u8fc7pip\u8f7b\u677e\u5b89\u88c5\u3002\u6587\u7ae0\u8be6\u7ec6\u4ecb\u7ecd\u4e86psutil\u7684\u6838\u5fc3\u529f\u80fd\u3001\u9ad8\u7ea7\u5e94\u7528\u6280\u5de7\u3001\u5e38\u89c1\u95ee\u9898\u89e3\u51b3\u65b9\u6848\uff0c\u5e76\u63d0\u4f9b\u4e86\u670d\u52a1\u5668\u76d1\u63a7\u811a\u672c\u548c\u81ea\u52a8\u5316\u8fd0\u7ef4\u4efb\u52a1\u7b49\u5b9e\u7528\u6848\u4f8b\uff0c\u662fPython\u5f00\u53d1\u8005\u8fdb\u884c\u7cfb\u7edf\u76d1\u63a7\u7684\u7406\u60f3\u5de5\u5177\u3002<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[81],"topic":[],"class_list":["post-49227","post","type-post","status-publish","format-standard","hentry","category-server","tag-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Python\u7cfb\u7edf\u76d1\u63a7\u5229\u5668\uff1apsutil\u8be6\u89e3 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/49227.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python\u7cfb\u7edf\u76d1\u63a7\u5229\u5668\uff1apsutil\u8be6\u89e3 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb661\u6b21\uff0c\u70b9\u8d5e30\u6b21\uff0c\u6536\u85cf7\u6b21\u3002psutil\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u8de8\u5e73\u53f0Python\u5e93\uff0c\u7528\u4e8e\u76d1\u63a7\u7cfb\u7edf\u8d44\u6e90\u548c\u8fdb\u7a0b\u7ba1\u7406\u3002\u5b83\u652f\u6301\u83b7\u53d6CPU\u3001\u5185\u5b58\u3001\u78c1\u76d8\u3001\u7f51\u7edc\u7b49\u7cfb\u7edf\u4fe1\u606f\uff0c\u5e76\u80fd\u8fdb\u884c\u8be6\u7ec6\u7684\u8fdb\u7a0b\u63a7\u5236\u3002psutil\u7684\u4e3b\u8981\u4f18\u52bf\u5305\u62ec\u7edf\u4e00\u7684\u8de8\u5e73\u53f0API\u3001\u5168\u9762\u7684\u76d1\u63a7\u529f\u80fd\u3001\u9ad8\u6548\u6027\u80fd\u548c\u6d3b\u8dc3\u7684\u793e\u533a\u652f\u6301\u3002\u8be5\u5e93\u9002\u7528\u4e8e\u670d\u52a1\u5668\u76d1\u63a7\u3001\u81ea\u52a8\u5316\u8fd0\u7ef4\u3001\u6027\u80fd\u5206\u6790\u7b49\u573a\u666f\uff0c\u53ef\u901a\u8fc7pip\u8f7b\u677e\u5b89\u88c5\u3002\u6587\u7ae0\u8be6\u7ec6\u4ecb\u7ecd\u4e86psutil\u7684\u6838\u5fc3\u529f\u80fd\u3001\u9ad8\u7ea7\u5e94\u7528\u6280\u5de7\u3001\u5e38\u89c1\u95ee\u9898\u89e3\u51b3\u65b9\u6848\uff0c\u5e76\u63d0\u4f9b\u4e86\u670d\u52a1\u5668\u76d1\u63a7\u811a\u672c\u548c\u81ea\u52a8\u5316\u8fd0\u7ef4\u4efb\u52a1\u7b49\u5b9e\u7528\u6848\u4f8b\uff0c\u662fPython\u5f00\u53d1\u8005\u8fdb\u884c\u7cfb\u7edf\u76d1\u63a7\u7684\u7406\u60f3\u5de5\u5177\u3002\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/49227.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-07-30T08:36:42+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"10 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/49227.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/49227.html\",\"name\":\"Python\u7cfb\u7edf\u76d1\u63a7\u5229\u5668\uff1apsutil\u8be6\u89e3 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-07-30T08:36:42+00:00\",\"dateModified\":\"2025-07-30T08:36:42+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/49227.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/49227.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/49227.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python\u7cfb\u7edf\u76d1\u63a7\u5229\u5668\uff1apsutil\u8be6\u89e3\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python\u7cfb\u7edf\u76d1\u63a7\u5229\u5668\uff1apsutil\u8be6\u89e3 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/49227.html","og_locale":"zh_CN","og_type":"article","og_title":"Python\u7cfb\u7edf\u76d1\u63a7\u5229\u5668\uff1apsutil\u8be6\u89e3 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb661\u6b21\uff0c\u70b9\u8d5e30\u6b21\uff0c\u6536\u85cf7\u6b21\u3002psutil\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u8de8\u5e73\u53f0Python\u5e93\uff0c\u7528\u4e8e\u76d1\u63a7\u7cfb\u7edf\u8d44\u6e90\u548c\u8fdb\u7a0b\u7ba1\u7406\u3002\u5b83\u652f\u6301\u83b7\u53d6CPU\u3001\u5185\u5b58\u3001\u78c1\u76d8\u3001\u7f51\u7edc\u7b49\u7cfb\u7edf\u4fe1\u606f\uff0c\u5e76\u80fd\u8fdb\u884c\u8be6\u7ec6\u7684\u8fdb\u7a0b\u63a7\u5236\u3002psutil\u7684\u4e3b\u8981\u4f18\u52bf\u5305\u62ec\u7edf\u4e00\u7684\u8de8\u5e73\u53f0API\u3001\u5168\u9762\u7684\u76d1\u63a7\u529f\u80fd\u3001\u9ad8\u6548\u6027\u80fd\u548c\u6d3b\u8dc3\u7684\u793e\u533a\u652f\u6301\u3002\u8be5\u5e93\u9002\u7528\u4e8e\u670d\u52a1\u5668\u76d1\u63a7\u3001\u81ea\u52a8\u5316\u8fd0\u7ef4\u3001\u6027\u80fd\u5206\u6790\u7b49\u573a\u666f\uff0c\u53ef\u901a\u8fc7pip\u8f7b\u677e\u5b89\u88c5\u3002\u6587\u7ae0\u8be6\u7ec6\u4ecb\u7ecd\u4e86psutil\u7684\u6838\u5fc3\u529f\u80fd\u3001\u9ad8\u7ea7\u5e94\u7528\u6280\u5de7\u3001\u5e38\u89c1\u95ee\u9898\u89e3\u51b3\u65b9\u6848\uff0c\u5e76\u63d0\u4f9b\u4e86\u670d\u52a1\u5668\u76d1\u63a7\u811a\u672c\u548c\u81ea\u52a8\u5316\u8fd0\u7ef4\u4efb\u52a1\u7b49\u5b9e\u7528\u6848\u4f8b\uff0c\u662fPython\u5f00\u53d1\u8005\u8fdb\u884c\u7cfb\u7edf\u76d1\u63a7\u7684\u7406\u60f3\u5de5\u5177\u3002","og_url":"https:\/\/www.wsisp.com\/helps\/49227.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-07-30T08:36:42+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"10 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/49227.html","url":"https:\/\/www.wsisp.com\/helps\/49227.html","name":"Python\u7cfb\u7edf\u76d1\u63a7\u5229\u5668\uff1apsutil\u8be6\u89e3 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-07-30T08:36:42+00:00","dateModified":"2025-07-30T08:36:42+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/49227.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/49227.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/49227.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"Python\u7cfb\u7edf\u76d1\u63a7\u5229\u5668\uff1apsutil\u8be6\u89e3"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/49227","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=49227"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/49227\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=49227"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=49227"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=49227"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=49227"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}