Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 50 additions & 21 deletions launcher2.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import atexit
import ctypes
import os
import subprocess
Expand Down Expand Up @@ -124,6 +125,44 @@ def scanStudentModels():
refreshList()
scanStudentModels()


def _run_hidden_taskkill(pid):
creation_flags = 0
if sys.platform == 'win32':
creation_flags = getattr(subprocess, 'CREATE_NO_WINDOW', 0x08000000)
subprocess.run(
['taskkill', '/F', '/PID', str(pid), '/T'],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
creationflags=creation_flags,
check=False
)


def stop_inference_process():
global p
if p is None:
return False

try:
if p.poll() is None:
_run_hidden_taskkill(p.pid)
try:
p.wait(timeout=1.5)
except Exception:
pass
except Exception:
try:
_run_hidden_taskkill(p.pid)
except Exception:
pass
finally:
p = None
return True


atexit.register(stop_inference_process)

def min_cutoff_mapper(value, revert=False):
"""
非线性映射函数:0-100整数 <-> 0-100浮点数
Expand Down Expand Up @@ -611,20 +650,19 @@ def OnLaunch(self, e):
f.close()
self.btnLaunch.SetLabelText('Working...')

if p is not None:
creation_flags = 0
if sys.platform == 'win32':
# CREATE_NO_WINDOW = 0x08000000
creation_flags = 0x08000000
subprocess.run(['taskkill', '/F', '/PID', str(p.pid), '/T'],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
creationflags=creation_flags)
p = None
if p is not None and p.poll() is None:
self.btnLaunch.Disable()
self.statusCtrl.Clear()
self.btnLaunch.SetLabelText("Save & Launch")
def _stop_async():
stop_inference_process()
wx.CallAfter(self.btnLaunch.Enable)

threading.Thread(target=_stop_async, daemon=True).start()
return
else:
# 如果启动器是用pythonw启动的,使用python.exe来启动main以便捕获控制台输出
p = None
python_exe = sys.executable
if 'pythonw' in python_exe.lower():
python_exe = python_exe.replace('pythonw.exe', 'python.exe').replace('pythonw', 'python')
Expand Down Expand Up @@ -764,7 +802,7 @@ def OnLaunch(self, e):
creation_flags = 0
if sys.platform == 'win32':
# CREATE_NO_WINDOW = 0x08000000
creation_flags = 0x08000000
creation_flags = getattr(subprocess, 'CREATE_NO_WINDOW', 0x08000000) | getattr(subprocess, 'CREATE_NEW_PROCESS_GROUP', 0x00000200)
p = subprocess.Popen(
run_args,
stdout=subprocess.PIPE,
Expand Down Expand Up @@ -792,16 +830,7 @@ def __init__(self, *args, **kw):
self.Bind(wx.EVT_CLOSE, self.OnClose)

def OnClose(self, e):
global p
if p is not None:
creation_flags = 0
if sys.platform == 'win32':
# CREATE_NO_WINDOW = 0x08000000
creation_flags = 0x08000000
subprocess.run(['taskkill', '/F', '/PID', str(p.pid), '/T'],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
creationflags=creation_flags)
stop_inference_process()
e.Skip()

def InitUi(self):
Expand Down
Loading