From ab1f85d192a2bf9abbe3eaa5c68a4145bc77f26b Mon Sep 17 00:00:00 2001 From: g1879 Date: Wed, 25 Oct 2023 20:00:28 +0800 Subject: [PATCH] =?UTF-8?q?auto=5Fport()=E6=94=AF=E6=8C=81=E5=A4=9A?= =?UTF-8?q?=E7=BA=BF=E7=A8=8B=EF=BC=9B=E6=94=AF=E6=8C=81=E6=9F=A5=E6=89=BE?= =?UTF-8?q?=E7=94=A8=E5=AE=8C=E7=AB=AF=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/_base/chromium_driver.py | 3 ++- DrissionPage/_configs/chromium_options.py | 26 ++++++++++++++++------ DrissionPage/_configs/chromium_options.pyi | 2 +- DrissionPage/_pages/chromium_base.py | 1 - 4 files changed, 22 insertions(+), 10 deletions(-) diff --git a/DrissionPage/_base/chromium_driver.py b/DrissionPage/_base/chromium_driver.py index 50dae8a..c593222 100644 --- a/DrissionPage/_base/chromium_driver.py +++ b/DrissionPage/_base/chromium_driver.py @@ -6,7 +6,7 @@ from json import dumps, loads from queue import Queue, Empty from threading import Thread, Event -from time import perf_counter +from time import perf_counter, sleep from requests import get from websocket import WebSocketTimeoutException, WebSocketException, WebSocketConnectionClosedException, \ @@ -84,6 +84,7 @@ class ChromiumDriver(object): if timeout is not None and perf_counter() > timeout: return {'error': {'message': 'timeout'}} + sleep(.02) continue except Exception: diff --git a/DrissionPage/_configs/chromium_options.py b/DrissionPage/_configs/chromium_options.py index 67dfe89..1ce072f 100644 --- a/DrissionPage/_configs/chromium_options.py +++ b/DrissionPage/_configs/chromium_options.py @@ -4,7 +4,9 @@ @Contact : g1879@qq.com """ from pathlib import Path +from shutil import rmtree from tempfile import gettempdir, TemporaryDirectory +from threading import Lock from .options_manage import OptionsManager from .._commons.tools import port_is_using, clean_folder @@ -471,24 +473,34 @@ class ChromiumOptions(object): class PortFinder(object): - used_port = [] + used_port = {} def __init__(self): self.tmp_dir = Path(gettempdir()) / 'DrissionPage' / 'TempFolder' self.tmp_dir.mkdir(parents=True, exist_ok=True) if not PortFinder.used_port: clean_folder(self.tmp_dir) + self._lock = Lock() def get_port(self): """查找一个可用端口 :return: 可以使用的端口和用户文件夹路径组成的元组 """ - for i in range(9600, 19800): - if i in PortFinder.used_port or port_is_using('127.0.0.1', i): - continue + with self._lock: + for i in range(9600, 19600): + if i in PortFinder.used_port: + continue + elif port_is_using('127.0.0.1', i): + PortFinder.used_port[i] = None + continue + path = TemporaryDirectory(dir=self.tmp_dir).name + PortFinder.used_port[i] = path + return i, path - path = TemporaryDirectory(dir=self.tmp_dir) - PortFinder.used_port.append(i) - return i, path.name + for i in range(9600, 19600): + if port_is_using('127.0.0.1', i): + continue + rmtree(PortFinder.used_port[i], ignore_errors=True) + return i, TemporaryDirectory(dir=self.tmp_dir).name raise OSError('未找到可用端口。') diff --git a/DrissionPage/_configs/chromium_options.pyi b/DrissionPage/_configs/chromium_options.pyi index 17d1906..161ff85 100644 --- a/DrissionPage/_configs/chromium_options.pyi +++ b/DrissionPage/_configs/chromium_options.pyi @@ -127,7 +127,7 @@ class ChromiumOptions(object): class PortFinder(object): - used_port: list = ... + used_port: dict = ... @staticmethod def get_port() -> Tuple[int, str]: ... diff --git a/DrissionPage/_pages/chromium_base.py b/DrissionPage/_pages/chromium_base.py index 5595dac..0b3a5e2 100644 --- a/DrissionPage/_pages/chromium_base.py +++ b/DrissionPage/_pages/chromium_base.py @@ -384,7 +384,6 @@ class ChromiumBase(BasePage): self.wait.load_complete() if self._scroll is None: self._scroll = ChromiumPageScroll(self) - self.set.scroll.smooth(False) return self._scroll @property