把主线程那一行剔掉再排
(每道题开头都有同一段:那张线程表 PSL,和把它切成字典列表的 rows()——每个字典有 pid / tid / nlwp / stat / cpu / comm 六项。)
找「算账」里最忙的线程:先剔掉主线程行(TID 等于 PID),再按 %CPU 排。看剔前剔后各是谁:
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 = [x for x in rows(PSL) if x["pid"] == 17]
before = max(r, key=lambda x: x["cpu"])["comm"]
workers = [x for x in r if x["tid"] != x["pid"]]
after = max(workers, key=lambda x: x["cpu"])["comm"]
print(before + "/" + after)
全部评论