修改quit()逻辑

This commit is contained in:
g1879 2024-02-06 16:36:44 +08:00
parent 7bb1e64a79
commit 193049428d
4 changed files with 16 additions and 5 deletions

View File

@ -210,7 +210,7 @@ class Browser(object):
ip, port = self.address.split(':') ip, port = self.address.split(':')
if ip not in ('127.0.0.1', 'localhost'): if ip not in ('127.0.0.1', 'localhost'):
return return
stop_process_on_port(port) stop_process_on_port(port, self.process_id)
return return
if self.process_id: if self.process_id:

View File

@ -157,7 +157,7 @@ def set_prefs(opt):
def set_flags(opt): def set_flags(opt):
"""处理启动配置中的prefs项,目前只能对已存在文件夹配置 """处理启动配置中的flags项
:param opt: ChromiumOptions :param opt: ChromiumOptions
:return: None :return: None
""" """

View File

@ -12,7 +12,7 @@ from tempfile import gettempdir, TemporaryDirectory
from threading import Lock from threading import Lock
from time import perf_counter from time import perf_counter
from psutil import process_iter, AccessDenied, NoSuchProcess, ZombieProcess from psutil import process_iter, AccessDenied, NoSuchProcess, ZombieProcess, Process
from .._configs.options_manage import OptionsManager from .._configs.options_manage import OptionsManager
from ..errors import (ContextLostError, ElementLostError, CDPError, PageDisconnectedError, NoRectError, from ..errors import (ContextLostError, ElementLostError, CDPError, PageDisconnectedError, NoRectError,
@ -182,9 +182,10 @@ def wait_until(function, kwargs=None, timeout=10):
raise TimeoutError raise TimeoutError
def stop_process_on_port(port): def stop_process_on_port(port, pid=None):
"""强制关闭某个端口内的进程 """强制关闭某个端口内的进程
:param port: 端口号 :param port: 端口号
:param pid: 进程号
:return: None :return: None
""" """
for proc in process_iter(['pid', 'connections']): for proc in process_iter(['pid', 'connections']):
@ -201,6 +202,16 @@ def stop_process_on_port(port):
except Exception as e: except Exception as e:
print(f"{proc.pid} {port}: {e}") print(f"{proc.pid} {port}: {e}")
if pid:
for p in Process(pid).children():
try:
p.terminate()
except:
pass
try:
Process(pid).terminate()
except:
pass
def configs_to_here(save_name=None): def configs_to_here(save_name=None):
"""把默认ini文件复制到当前目录 """把默认ini文件复制到当前目录

View File

@ -42,7 +42,7 @@ def get_hwnds_from_pid(pid: Union[str, int], title: str) -> list: ...
def wait_until(function: callable, kwargs: dict = None, timeout: float = 10): ... def wait_until(function: callable, kwargs: dict = None, timeout: float = 10): ...
def stop_process_on_port(port: Union[int, str]) -> None: ... def stop_process_on_port(port: Union[int, str], pid: int = None) -> None: ...
def configs_to_here(file_name: Union[Path, str] = None) -> None: ... def configs_to_here(file_name: Union[Path, str] = None) -> None: ...