Server IP : 85.214.239.14 / Your IP : 18.119.255.170 Web Server : Apache/2.4.62 (Debian) System : Linux h2886529.stratoserver.net 4.9.0 #1 SMP Tue Jan 9 19:45:01 MSK 2024 x86_64 User : www-data ( 33) PHP Version : 7.4.18 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare, MySQL : OFF | cURL : OFF | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : OFF Directory : /proc/3/root/proc/self/root/usr/share/doc/python3-rich/examples/ |
Upload File : |
"""Lite simulation of the top linux command.""" import datetime import random import sys import time from dataclasses import dataclass from rich import box from rich.console import Console from rich.live import Live from rich.table import Table if sys.version_info >= (3, 8): from typing import Literal else: from typing_extensions import Literal @dataclass class Process: pid: int command: str cpu_percent: float memory: int start_time: datetime.datetime thread_count: int state: Literal["running", "sleeping"] @property def memory_str(self) -> str: if self.memory > 1e6: return f"{int(self.memory/1e6)}M" if self.memory > 1e3: return f"{int(self.memory/1e3)}K" return str(self.memory) @property def time_str(self) -> str: return str(datetime.datetime.now() - self.start_time) def generate_process(pid: int) -> Process: return Process( pid=pid, command=f"Process {pid}", cpu_percent=random.random() * 20, memory=random.randint(10, 200) ** 3, start_time=datetime.datetime.now() - datetime.timedelta(seconds=random.randint(0, 500) ** 2), thread_count=random.randint(1, 32), state="running" if random.randint(0, 10) < 8 else "sleeping", ) def create_process_table(height: int) -> Table: processes = sorted( [generate_process(pid) for pid in range(height)], key=lambda p: p.cpu_percent, reverse=True, ) table = Table( "PID", "Command", "CPU %", "Memory", "Time", "Thread #", "State", box=box.SIMPLE ) for process in processes: table.add_row( str(process.pid), process.command, f"{process.cpu_percent:.1f}", process.memory_str, process.time_str, str(process.thread_count), process.state, ) return table console = Console() with Live(console=console, screen=True, auto_refresh=False) as live: while True: live.update(create_process_table(console.size.height - 4), refresh=True) time.sleep(1)