diff --git a/DrissionPage/_base/browser.py b/DrissionPage/_base/browser.py index 89fd915..ce41cff 100644 --- a/DrissionPage/_base/browser.py +++ b/DrissionPage/_base/browser.py @@ -210,7 +210,7 @@ class Browser(object): ip, port = self.address.split(':') if ip not in ('127.0.0.1', 'localhost'): return - stop_process_on_port(port) + stop_process_on_port(port, self.process_id) return if self.process_id: diff --git a/DrissionPage/_functions/browser.py b/DrissionPage/_functions/browser.py index 4f79269..2fbc5c7 100644 --- a/DrissionPage/_functions/browser.py +++ b/DrissionPage/_functions/browser.py @@ -157,7 +157,7 @@ def set_prefs(opt): def set_flags(opt): - """处理启动配置中的prefs项,目前只能对已存在文件夹配置 + """处理启动配置中的flags项 :param opt: ChromiumOptions :return: None """ diff --git a/DrissionPage/_functions/tools.py b/DrissionPage/_functions/tools.py index ee4ba25..dfd2340 100644 --- a/DrissionPage/_functions/tools.py +++ b/DrissionPage/_functions/tools.py @@ -12,7 +12,7 @@ from tempfile import gettempdir, TemporaryDirectory from threading import Lock 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 ..errors import (ContextLostError, ElementLostError, CDPError, PageDisconnectedError, NoRectError, @@ -182,9 +182,10 @@ def wait_until(function, kwargs=None, timeout=10): raise TimeoutError -def stop_process_on_port(port): +def stop_process_on_port(port, pid=None): """强制关闭某个端口内的进程 :param port: 端口号 + :param pid: 进程号 :return: None """ for proc in process_iter(['pid', 'connections']): @@ -201,6 +202,16 @@ def stop_process_on_port(port): except Exception as 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): """把默认ini文件复制到当前目录 diff --git a/DrissionPage/_functions/tools.pyi b/DrissionPage/_functions/tools.pyi index f6e13f5..f6b55c0 100644 --- a/DrissionPage/_functions/tools.pyi +++ b/DrissionPage/_functions/tools.pyi @@ -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 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: ...