1
0
mirror of https://gitee.com/sui-feng-cb/AzurLaneAutoScript1 synced 2026-03-12 03:58:22 +08:00

Opt: LaunchPath insted of CmdLine

This commit is contained in:
hgjazhgj
2023-05-27 21:22:33 +08:00
parent 742e34c8d0
commit b4af10b56b
12 changed files with 88 additions and 25 deletions

View File

@@ -7,11 +7,18 @@ from threading import Thread
class HeadlessCliApplication:
"""Wrap a cli application to provide programmable interactive access"""
def __init__(self, cmd):
def __init__(self, launch, halt = ""):
self.pipe = Popen(
shlex.split(cmd, posix=os.name=="posix"),
[launch],
stdin=PIPE, stdout=PIPE, stderr=STDOUT, text=True
)
self.orphanSlayer = Popen([
"python",
os.path.join(os.path.dirname(__file__), "orphanSlayer.py"),
str(os.getpid()),
str(self.pipe.pid),
halt,
])
def f():
while True:

View File

@@ -0,0 +1,47 @@
import os
import time
def is_process_exist(pid):
try:
os.kill(pid, 0)
except OSError:
return False
else:
return True
def orphanSlayer(ppid, spid, prekill = ""):
"""
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曰「你可以通过经常拉屎来结交朋友
"""
while is_process_exist(ppid):
if not is_process_exist(spid):
return
time.sleep(1)
if prekill:
os.system(prekill)
os.kill(spid, 9)
if __name__ == "__main__":
# python orphanSlayer.py 114 514 "docker stop ..."
import sys
orphanSlayer(int(sys.argv[1]), int(sys.argv[2]), sys.argv[3])