In Windows, create new console and hide it

Minor Correction:
* logging
* change status return type
This commit is contained in:
kale4eat
2024-03-06 11:19:55 +09:00
parent 2ab025d5fe
commit 45c6bde2e7
2 changed files with 19 additions and 7 deletions

View File

@@ -74,8 +74,11 @@ def initialize(port: int = WOKER_PORT):
args = [sys.executable, "-m", worker_pkg_path, "--port", str(port)] args = [sys.executable, "-m", worker_pkg_path, "--port", str(port)]
# new session, new process group # new session, new process group
if sys.platform.startswith("win"): if sys.platform.startswith("win"):
cf = subprocess.DETACHED_PROCESS | subprocess.CREATE_NEW_PROCESS_GROUP # type: ignore cf = subprocess.CREATE_NEW_CONSOLE | subprocess.CREATE_NEW_PROCESS_GROUP # type: ignore
subprocess.Popen(args, creationflags=cf) si = subprocess.STARTUPINFO() # type: ignore
si.dwFlags |= subprocess.STARTF_USESHOWWINDOW # type: ignore
si.wShowWindow = subprocess.SW_HIDE # type: ignore
subprocess.Popen(args, creationflags=cf, startupinfo=si)
else: else:
# align with Windows behavior # align with Windows behavior
# start_new_session is same as specifying setsid in preexec_fn # start_new_session is same as specifying setsid in preexec_fn
@@ -107,7 +110,7 @@ def terminate():
# repare for unexpected errors # repare for unexpected errors
try: try:
if WORKER_CLIENT.status().get("client-count") == 1: if WORKER_CLIENT.status() == 1:
WORKER_CLIENT.quit_server() WORKER_CLIENT.quit_server()
except Exception as e: except Exception as e:
logger.error(e) logger.error(e)

View File

@@ -38,9 +38,18 @@ class WorkerClient:
return response.get("return") return response.get("return")
def status(self): def status(self):
send_data(self.sock, {"request-type": RequestType.STATUS}) data = {"request-type": RequestType.STATUS}
return receive_data(self.sock) logger.trace(f"client sends request: {data}")
send_data(self.sock, data)
logger.trace("client sent request successfully")
response = receive_data(self.sock)
logger.trace(f"client received response: {response}")
return response.get("client-count")
def quit_server(self): def quit_server(self):
send_data(self.sock, {"request-type": RequestType.QUIT_SERVER}) data = {"request-type": RequestType.QUIT_SERVER}
receive_data(self.sock) logger.trace(f"client sends request: {data}")
send_data(self.sock, data)
logger.trace("client sent request successfully")
response = receive_data(self.sock)
logger.trace(f"client received response: {response}")