优化ChromiumDriver超时逻辑

This commit is contained in:
g1879 2023-10-24 17:45:20 +08:00
parent 301569a9cb
commit d587ca6095
2 changed files with 9 additions and 15 deletions

View File

@ -6,6 +6,7 @@
from json import dumps, loads from json import dumps, loads
from queue import Queue, Empty from queue import Queue, Empty
from threading import Thread, Event from threading import Thread, Event
from time import perf_counter
from requests import get from requests import get
from websocket import WebSocketTimeoutException, WebSocketException, WebSocketConnectionClosedException, \ from websocket import WebSocketTimeoutException, WebSocketException, WebSocketConnectionClosedException, \
@ -45,7 +46,7 @@ class ChromiumDriver(object):
def _send(self, message, timeout=None): def _send(self, message, timeout=None):
"""发送信息到浏览器,并返回浏览器返回的信息 """发送信息到浏览器,并返回浏览器返回的信息
:param message: 发送给浏览器的数据 :param message: 发送给浏览器的数据
:param timeout: 超时时间 :param timeout: 超时时间为None表示无限
:return: 浏览器返回的数据 :return: 浏览器返回的数据
""" """
if 'id' not in message: if 'id' not in message:
@ -64,10 +65,8 @@ class ChromiumDriver(object):
print(f'发> {message_json}') print(f'发> {message_json}')
break break
if not isinstance(timeout, (int, float)) or timeout > 1: if timeout is not None:
q_timeout = 1 timeout = perf_counter() + timeout
else:
q_timeout = timeout / 2.0
try: try:
self.method_results[message['id']] = Queue() self.method_results[message['id']] = Queue()
@ -75,19 +74,14 @@ class ChromiumDriver(object):
while not self._stopped.is_set(): while not self._stopped.is_set():
try: try:
if isinstance(timeout, (int, float)): return self.method_results[message['id']].get_nowait()
if timeout < q_timeout:
q_timeout = timeout
timeout -= q_timeout
return self.method_results[message['id']].get(timeout=q_timeout)
except Empty: except Empty:
if self.has_alert: if self.has_alert:
return {'error': {'message': 'alert exists'}, 'type': 'alert_exists'} return {'error': {'message': 'alert exists'}, 'type': 'alert_exists'}
if isinstance(timeout, (int, float)) and timeout <= 0: if timeout is not None and perf_counter() > timeout:
raise TimeoutError(f"调用{message['method']}超时。") return {'error': {'message': 'timeout'}}
continue continue

View File

@ -1306,7 +1306,7 @@ def run_js(page_or_ele, script, as_expr=False, timeout=None, args=None):
try: try:
if as_expr: if as_expr:
res = page.run_cdp('Runtime.evaluate', expression=script, returnByValue=False, res = page.run_cdp('Runtime.evaluate', expression=script, returnByValue=False,
awaitPromise=True, userGesture=True, timeout=timeout * 1000) awaitPromise=True, userGesture=True, _timeout=timeout)
else: else:
args = args or () args = args or ()
@ -1314,7 +1314,7 @@ def run_js(page_or_ele, script, as_expr=False, timeout=None, args=None):
script = f'function(){{{script}}}' script = f'function(){{{script}}}'
res = page.run_cdp('Runtime.callFunctionOn', functionDeclaration=script, objectId=obj_id, res = page.run_cdp('Runtime.callFunctionOn', functionDeclaration=script, objectId=obj_id,
arguments=[convert_argument(arg) for arg in args], returnByValue=False, arguments=[convert_argument(arg) for arg in args], returnByValue=False,
awaitPromise=True, userGesture=True) awaitPromise=True, userGesture=True, _timeout=timeout)
except ContextLossError: except ContextLossError:
if is_page: if is_page: