把表切开
(每道题开头都有同一段:那张线程表 PSL,和把它切成字典列表的 rows()——每个字典有 pid / tid / nlwp / stat / cpu / comm 六项。)
rows() 把表切成字典列表。看一共几行、几个不同的 PID、哪一行带 R:
PSL = '''PID TID NLWP STAT %CPU COMM
1 1 1 Ss 0.0 bash
12 12 1 S 0.0 收数据.sh
17 17 4 Ssl 49.8 算账
17 18 4 Rsl 49.6 算账-核对
17 19 4 Ssl 0.0 算账-写盘
17 20 4 Ssl 0.1 算账-心跳
21 21 2 Ssl 0.0 通知
21 22 2 Ssl 0.0 通知-等待'''
def rows(text):
out = []
for line in text.split("\n")[1:]:
pid, tid, nlwp, stat, cpu, comm = line.split()
out.append({"pid": int(pid), "tid": int(tid), "nlwp": int(nlwp), "stat": stat, "cpu": float(cpu), "comm": comm})
return out
r = rows(PSL)
pids = sorted({x["pid"] for x in r})
running = [x["comm"] for x in r if x["stat"].startswith("R")]
print(str(len(r)) + "/" + str(len(pids)) + "/" + ",".join(running))
全部评论