0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

残プロ 第-39回 ~コンソールで簡易タスクマネージャー~

Posted at

手軽にCPUとメモリ使用率を確認したい

コンソール上で各種使用率を確認するにはtypeperfコマンドを叩きます.ただ,出てくる数字が4桁(+小数点以下6桁)で非常に分かりにくい.今回はシーケンスバーをコンソール上に実現し,一目でわかる工夫を加えました.ぜひお試しあれ.

TaskManeger.py
import subprocess
import re

cmd_cls = "cls"
_ = subprocess.run(cmd_cls, shell=True)

cmd_totalmemory = "wmic ComputerSystem get TotalPhysicalMemory"
output = subprocess.run(cmd_totalmemory, shell=True, stdout=subprocess.PIPE, text=True)
memory_total = int(re.findall('[0-9]+', output.stdout)[0]) // 10**6

print("USED   0%       50%       100%")
print("----   +---------+---------+")
print("CPU                         ")
print("MEMORY                      ")

while(True):
    cmd_used = "typeperf -sc 1 \"\\processor(_Total)\\% Processor Time\" \"\\Memory\\Available MBytes\"";
    output = subprocess.run(cmd_used, shell=True, stdout=subprocess.PIPE, text=True)
    cpu, memory = re.findall('[0-9]+\.[0-9]{6,6}', output.stdout)
    cpu_percent = int(float(cpu))
    memory_percent = int(float(memory) / memory_total * 100)

    print("\033[2F" + "CPU                         ")
    print("\033[1F" + "CPU    " + "#" * (cpu_percent//5))
    print("MEMORY                      ")
    print("\033[1F" + "MEMORY " + "#" * (memory_percent//5))

例に漏れず,ANSIエスケープシーケンスを利用しています.コンソール制御のコツとしてはカーソル基準行を決めておいて,逐一変更したい行に移動することで混乱を回避できます.

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?