This commit is contained in:
2025-11-27 08:57:45 +08:00
parent ddae7ae475
commit 85f39f30fa

View File

@@ -1,4 +1,5 @@
import os import os
from re import T
import time import time
import aiohttp import aiohttp
import asyncio import asyncio
@@ -39,38 +40,6 @@ def retry(max_retries: int = 3, delay: float = 1.0, backoff: float = 1.0):
return decorator return decorator
def async_retry(max_retries: int = 3, delay: float = 1.0, backoff: float = 1.0):
"""
支持异步函数的通用重试装饰器
:param max_retries: 最大重试次数
:param delay: 每次重试的初始延迟(秒)
:param backoff: 每次重试延迟的递增倍数
"""
def decorator(func):
@wraps(func)
async def wrapper(*args, **kwargs):
retries = 0
current_delay = delay
while retries < max_retries:
try:
return await func(*args, **kwargs) # 直接执行原始方法
except Exception as e:
retries += 1
if retries >= max_retries:
logger.warning(f"函数 {func.__name__} 在尝试了 {max_retries} 次后失败,错误信息: {e}")
return None # 重试次数用尽后返回 None
logger.warning(f"正在重试 {func.__name__} {retries + 1}/{max_retries} 因错误: {e}")
await asyncio.sleep(current_delay) # 异步延迟
current_delay *= backoff # 根据backoff递增延迟
return None # 三次重试仍未成功,返回 None
return wrapper
return decorator
# 比特浏览器模块 # 比特浏览器模块
class BitBrowser: class BitBrowser:
@@ -199,11 +168,11 @@ class BitBrowser:
res = requests.post(url, json=data, headers=headers).json() res = requests.post(url, json=data, headers=headers).json()
if not res.get('success'): if not res.get('success'):
raise Exception(res) raise Exception(res)
# 等待3秒
time.sleep(3)
bol = self.bit_browser_status(pk) bol = self.bit_browser_status(pk)
if bol: if bol:
# 等待5秒 raise Exception(f'浏览器ID {pk} 未正常关闭, 等待3秒后重试')
time.sleep(5)
raise Exception(f'浏览器ID {pk} 未正常关闭')
return True return True
# 删除比特币浏览器 # 删除比特币浏览器
@@ -339,9 +308,14 @@ async def main():
print(f'='*50) print(f'='*50)
jc += 1 jc += 1
def main2():
bit = BitBrowser()
bit_browser = BitBrowser() browser_id = '5ba9eb974c7c45e2bb086585c75f70e8'
# 关闭浏览器
res = bit.bit_browser_close(browser_id)
print(res)
# if __name__ == '__main__': # if __name__ == '__main__':
# asyncio.run(main()) # main2()
bit_browser = BitBrowser()