retry
This commit is contained in:
1 parent
7d5c8c57b7
commit
1d732844e3
2 files changed
+30
-15
No files matched your search
+3
-1
@@ -26,7 +26,9 @@ class DatabaseManager:
|
|||||||
password = os.getenv('MYSQL_PASSWORD')
|
password = os.getenv('MYSQL_PASSWORD')
|
||||||
database = os.getenv('MYSQL_DB')
|
database = os.getenv('MYSQL_DB')
|
||||||
self._engine = create_engine(
|
self._engine = create_engine(
|
||||||
f"mysql+pymysql://{user}:{password}@{host}:{port}/{database}?charset=utf8mb4"
|
f"mysql+pymysql://{user}:{password}@{host}:{port}/{database}?charset=utf8mb4",
|
||||||
|
pool_pre_ping=True,
|
||||||
|
pool_recycle=3600
|
||||||
)
|
)
|
||||||
return self._engine
|
return self._engine
|
||||||
|
|
||||||
|
|||||||
+27
-14
@@ -1,6 +1,7 @@
|
|||||||
"""从Binance获取加密货币OHLCV数据"""
|
"""从Binance获取加密货币OHLCV数据"""
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
import time
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Optional, Literal
|
from typing import Optional, Literal
|
||||||
@@ -69,7 +70,8 @@ class BinanceDataFetcher:
|
|||||||
timeframe: Literal['1h', '15m'],
|
timeframe: Literal['1h', '15m'],
|
||||||
start_date: Optional[str] = None,
|
start_date: Optional[str] = None,
|
||||||
end_date: Optional[str] = None,
|
end_date: Optional[str] = None,
|
||||||
use_cache: bool = True
|
use_cache: bool = True,
|
||||||
|
max_retries: int = 3 # [新增] 最大重试次数参数
|
||||||
) -> pd.DataFrame:
|
) -> pd.DataFrame:
|
||||||
"""获取加密货币对的OHLCV数据
|
"""获取加密货币对的OHLCV数据
|
||||||
|
|
||||||
@@ -128,19 +130,30 @@ class BinanceDataFetcher:
|
|||||||
|
|
||||||
print(f"获取 {symbol} {timeframe} 数据,时间范围:{start_dt.date()} 至 {end_dt.date()}...")
|
print(f"获取 {symbol} {timeframe} 数据,时间范围:{start_dt.date()} 至 {end_dt.date()}...")
|
||||||
|
|
||||||
# 获取K线数据
|
klines = []
|
||||||
try:
|
for attempt in range(max_retries):
|
||||||
klines = self.client.get_historical_klines(
|
try:
|
||||||
symbol=pair,
|
klines = self.client.get_historical_klines(
|
||||||
interval=interval,
|
symbol=pair,
|
||||||
start_str=str(int(start_dt.timestamp() * 1000)),
|
interval=interval,
|
||||||
end_str=str(int(end_dt.timestamp() * 1000))
|
start_str=str(int(start_dt.timestamp() * 1000)),
|
||||||
)
|
end_str=str(int(end_dt.timestamp() * 1000))
|
||||||
except Exception as e:
|
)
|
||||||
if not df.empty:
|
# 如果成功获取,跳出循环
|
||||||
print(f"⚠ API请求失败 ({e}),返回数据库现有数据")
|
break
|
||||||
return df
|
except Exception as e:
|
||||||
raise e
|
# 如果是最后一次尝试,且依然失败
|
||||||
|
if attempt == max_retries - 1:
|
||||||
|
print(f"❌ 第 {attempt + 1} 次尝试失败,已达到最大重试次数。错误: {e}")
|
||||||
|
if not df.empty:
|
||||||
|
print(f"⚠ API请求最终失败,返回数据库现有数据")
|
||||||
|
return df
|
||||||
|
raise e # 如果没有缓存数据,抛出异常
|
||||||
|
|
||||||
|
# 计算等待时间 (指数退避: 2s, 4s, 8s...)
|
||||||
|
wait_time = 2 * (2 ** attempt)
|
||||||
|
print(f"⚠ 请求失败 (尝试 {attempt + 1}/{max_retries}),{wait_time}秒后重试... 错误: {e}")
|
||||||
|
time.sleep(wait_time)
|
||||||
|
|
||||||
if not klines:
|
if not klines:
|
||||||
if not df.empty:
|
if not df.empty:
|
||||||
|
|||||||
Reference in new issue
Block a user