{"id":3390,"date":"2023-05-18T08:05:51","date_gmt":"2023-05-18T00:05:51","guid":{"rendered":"http:\/\/192.168.31.200\/?p=3390"},"modified":"2023-05-18T08:05:52","modified_gmt":"2023-05-18T00:05:52","slug":"%e4%bd%bf%e7%94%a8python-psutil%e5%ba%93%e8%8e%b7%e5%8f%96cpu%e3%80%81%e5%86%85%e5%ad%98%e3%80%81%e7%a3%81%e7%9b%98%e3%80%81%e7%bd%91%e7%bb%9c%e3%80%81%e8%bf%9b%e7%a8%8b%e4%bf%a1%e6%81%af","status":"publish","type":"post","link":"http:\/\/g1n29wqq.ipyingshe.net:5347\/?p=3390","title":{"rendered":"\u4f7f\u7528Python psutil\u5e93\u83b7\u53d6CPU\u3001\u5185\u5b58\u3001\u78c1\u76d8\u3001\u7f51\u7edc\u3001\u8fdb\u7a0b\u4fe1\u606f"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>import psutil\r\n\r\ndef line(n):  #\u8f93\u51fa\u6307\u5b9a\u6570\u91cf\u7684\u6a2a\u7ebf\r\n    print(\"-\"*n)\r\n\r\n# \u83b7\u53d6CPU\u4fe1\u606f\r\nprint(\"CPU\u4fe1\u606f\")\r\nline(10)\r\nprint(\"CPU\u903b\u8f91\u6570\u91cf:\", psutil.cpu_count())  # CPU\u903b\u8f91\u6570\u91cf\r\nprint(\"CPU\u7269\u7406\u6838\u5fc3:\", psutil.cpu_count(logical=False))  # CPU\u7269\u7406\u6838\u5fc3\r\nprint(\"CPU \u4f7f\u7528\u7387\",psutil.cpu_percent(),\"%\")\r\nprint(\"cpu \u72b6\u6001\",psutil.cpu_stats())\r\nline(10)\r\n\r\n# \u83b7\u53d6\u5185\u5b58\u4fe1\u606f\r\nprint(\"\u83b7\u53d6\u5185\u5b58\u4fe1\u606f\")\r\nline(10)\r\nmem = psutil.virtual_memory()\r\nprint(\"\u603b\u5185\u5b58:\", mem.total\/1024\/1024, \"MB\")\r\nprint(\"\u5df2\u7528\u5185\u5b58:\", mem.used\/1024\/1024, \"MB\")\r\nprint(\"\u7a7a\u95f2\u5185\u5b58:\", mem.free\/1024\/1024, \"MB\")  #or use print(\"\u7a7a\u95f2\u5185\u5b58:\",mem.available \/ 1024 \/ 1024, \"MB\")\r\nprint(\"\u4f7f\u7528\u5185\u5b58\u5360\u6bd4:\", mem.percent)\r\nline(10)\r\n# \u83b7\u53d6\u78c1\u76d8\u4fe1\u606f\r\nprint(\"\u83b7\u53d6\u78c1\u76d8\u4fe1\u606f\")\r\nline(10)\r\ndisk = psutil.disk_partitions()\r\nprint(\"\u78c1\u76d8\u5206\u533a\u4fe1\u606f\uff1a\", disk)\r\ndisk_usage = psutil.disk_usage('\/')\r\nprint(\"\u603b\u78c1\u76d8\u7a7a\u95f4\uff1a\", disk_usage.total\/1024\/1024,\"MB\")\r\nprint(\"\u78c1\u76d8\u4f7f\u7528\u60c5\u51b5\uff1a\", disk_usage.used\/1024\/1024,\"MB\")\r\nprint(\"\u78c1\u76d8\u7a7a\u95f2\u60c5\u51b5\uff1a\", disk_usage.free\/1024\/1024,\"MB\")\r\ndisk_io = psutil.disk_io_counters()\r\nprint(\"\u78c1\u76d8IO\uff1a\", disk_io)\r\nline(10)\r\n\r\n# \u83b7\u53d6\u7f51\u7edc\u4fe1\u606f\r\nprint(\"\u83b7\u53d6\u7f51\u7edc\u4fe1\u606f\")\r\nline(10)\r\nnet_io = psutil.net_io_counters()\r\nprint(\"\u7f51\u7edcIO\uff1a\", net_io)\r\n\r\n#\u83b7\u53d6\u5f53\u524d\u8fd0\u884c\u7684\u6240\u6709\u8fdb\u7a0b\r\nprint(\"\u83b7\u53d6\u5f53\u524d\u8fd0\u884c\u7684\u6240\u6709\u8fdb\u7a0b\")\r\nline(10)\r\nprocesses = psutil.process_iter()\r\nfor process in  processes:   #\u8f93\u51fa\u4fe1\u606f\u91cf\u5de8\u5927\uff01\r\n    print(\"\u8fdb\u7a0bIO\uff1a\",process.pid)\r\n    print(\"\u8fdb\u7a0b\u540d\u79f0\uff1a\",process.name())\r\nline(10)\r\n#\u83b7\u53d6\u7f51\u7edc\u4fe1\u606f\r\nprint(\"\u83b7\u53d6\u7f51\u7edc\u4fe1\u606f\")\r\nline(10)\r\n# \u83b7\u53d6\u7f51\u7edc\u4fe1\u606f\r\nnet_io = psutil.net_io_counters()\r\nprint(\"\u7f51\u7edcIO\uff1a\", net_io)\r\n# \u83b7\u53d6\u7f51\u7edc\u63a5\u53e3\u4fe1\u606f\r\ncc = psutil.net_if_addrs()\r\nprint(\"\u7f51\u7edc\u63a5\u53e3\u4fe1\u606f\uff1a\", cc)\r\n# \u83b7\u53d6\u7f51\u7edc\u63a5\u53e3\u72b6\u6001\u4fe1\u606f\r\ndd = psutil.net_if_stats()\r\nprint(\"\u7f51\u7edc\u63a5\u53e3\u72b6\u6001\u4fe1\u606f\uff1a\", dd)\r\n# \u83b7\u53d6\u6240\u6709\u7f51\u7edc\u8fde\u63a5\u4fe1\u606f\r\nee = psutil.net_connections()\r\nprint(\"\u6240\u6709\u7f51\u7edc\u8fde\u63a5\u4fe1\u606f\uff1a\", ee)\r\n\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,10],"tags":[],"class_list":["post-3390","post","type-post","status-publish","format-standard","hentry","category-2","category-python"],"_links":{"self":[{"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/3390","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3390"}],"version-history":[{"count":1,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/3390\/revisions"}],"predecessor-version":[{"id":3391,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/3390\/revisions\/3391"}],"wp:attachment":[{"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3390"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3390"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3390"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}