From ecb70926a79a3e285397696c608223e73d69c5d9 Mon Sep 17 00:00:00 2001 From: rgarcia-herrera Date: Fri, 25 Oct 2024 22:11:20 -0600 Subject: [PATCH] =?UTF-8?q?ac=C3=A1=20un=20primordio=20del=20random=20walk?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ps_random_walk.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 ps_random_walk.py diff --git a/ps_random_walk.py b/ps_random_walk.py new file mode 100644 index 0000000..b4775d9 --- /dev/null +++ b/ps_random_walk.py @@ -0,0 +1,29 @@ +import psutil +import random +from time import sleep + + +def reporta(p): + binario = bin(p.pid).replace('0b', '') + return p.pid, binario, binario[0:7], len(str(p.pid)), len(p.name()) + + +p = psutil.Process(pid=random.choice(psutil.pids())) +print('random chose', reporta(p)) + + +while True: + sleep(1) + children = p.children() + + if children: + p = psutil.Process(pid=random.choice([subp.pid + for subp in + children])) + print("\t↳", reporta(p)) + + else: + p = psutil.Process(pid=random.choice(psutil.pids())) + print('→', reporta(p)) + +