1
0
mirror of https://gitee.com/sui-feng-cb/AzurLaneAutoScript1 synced 2026-03-12 17:57:01 +08:00
Files
AzurLaneAutoScript/submodule/AlasFpyBridge/module/utils/orphanSlayer.py

55 lines
1.6 KiB
Python
Raw Normal View History

2023-05-27 21:22:33 +08:00
import os
2023-05-27 22:16:37 +08:00
import platform
2023-05-27 21:22:33 +08:00
import time
2023-05-27 22:16:37 +08:00
if platform.system() == "Windows":
2023-05-28 03:17:47 +08:00
2023-05-27 22:16:37 +08:00
def isProcessExist(pid):
with os.popen(f'tasklist /NH /FI "PID eq {pid}"') as p:
return p.read()[0] == "\n"
2023-05-28 03:17:47 +08:00
2023-05-27 22:16:37 +08:00
else:
2023-05-28 03:17:47 +08:00
2023-05-27 22:16:37 +08:00
def isProcessExist(pid):
try:
os.kill(pid, 0)
except OSError:
return False
else:
return True
2023-05-27 21:22:33 +08:00
def main(ppid, spid, kill=""):
2023-05-27 21:22:33 +08:00
"""
module.webui.process_manager.ProcessManager.stop() uses kill() to stop subprocess
and to a large extent it cannot be changed to terminate(), see #883
In *nix systems, process created by subprosecc.Popen does not exit with the parent process,
and there are no options of daemon=True, etc.
So there need to be some way to ensure sub-subprocess to be killed other than capturing SIGTERM,
such as:
1. Set subprocess to a new process group and killpg() in ProcessManager.stop()
2. Try to terminate() or send other signals before kill()
3. Make sub-subprocess end itself when it does not receive a heartbeat
All of these seemingly elegant solutions require invasive changes,
therefore, I choose to open another process, once the father dead, kill the son as well
So called orphanSlayer
Lme曰你可以通过经常拉屎来结交朋友
"""
2023-05-27 22:16:37 +08:00
while isProcessExist(ppid):
2023-05-27 21:22:33 +08:00
time.sleep(1)
2023-05-27 23:12:07 +08:00
if kill:
2023-05-27 23:30:21 +08:00
os.system(f"{kill} {spid}")
2023-05-27 23:12:07 +08:00
else:
os.kill(spid, 9)
2023-05-27 21:22:33 +08:00
if __name__ == "__main__":
# python orphanSlayer.py 114 514 "docker stop ..."
import sys
main(int(sys.argv[1]), int(sys.argv[2]), sys.argv[3])