Files
sanguo_vnpy_v2/scripts/harvest_zombies.py
T

64 lines
2.0 KiB
Python

# -*- coding: utf-8 -*-
"""周一晨僵尸收割(2026-09-14 07:2x): 杀 12 个 wedged 引擎子进程, supervisor 自动重拉。
不动 supervisor 本体。用法: python -X utf8 C:\\sanguo_bigqmt\\harvest_zombies.py [status]
"""
from __future__ import annotations
import sys
import time
def _procs():
import psutil
live_sup, shadow_sup, children = [], [], []
for p in psutil.process_iter(["pid", "name", "cmdline", "create_time"]):
if "python" not in (p.info["name"] or "").lower():
continue
cmd = p.info["cmdline"] or []
j = " ".join(cmd)
if "sanguo_live" in j and "--supervisor" in j:
live_sup.append((p.info["pid"], _ts(p)))
elif "runner_live" in j:
children.append(p)
elif "sanguo_trader.shadow" in j and "--auto" in j:
shadow_sup.append((p.info["pid"], _ts(p)))
elif "sanguo_trader.shadow" in j:
children.append(p)
return live_sup, shadow_sup, children
def _ts(p):
return time.strftime("%m-%d %H:%M", time.localtime(p.info["create_time"]))
def main():
status_only = len(sys.argv) > 1 and sys.argv[1] == "status"
live_sup, shadow_sup, children = _procs()
print("SUP live=%s shadow=%s" % (live_sup, shadow_sup))
print("CHILDREN n=%d (zombies, expect start=09-11/09-12):" % len(children))
for p in children:
print(" pid=%s start=%s" % (p.info["pid"], _ts(p)))
if status_only:
return
if not children:
print("NO_CHILDREN_TO_HARVEST")
for p in children:
try:
p.terminate()
print("TERM", p.info["pid"])
except Exception as e:
print("TERM_FAIL", p.info["pid"], e)
t0 = time.time()
while time.time() - t0 < 60:
if not _procs()[2]:
print("HARVEST_DONE %.0fs" % (time.time() - t0))
break
time.sleep(5)
else:
print("HARVEST_TIMEOUT residual=%d" % len(_procs()[2]))
raise SystemExit(1)
if __name__ == "__main__":
main()