|
|
@@ -4,6 +4,7 @@
|
|
|
import hashlib
|
|
|
import json
|
|
|
import logging
|
|
|
+import threading
|
|
|
import time
|
|
|
from datetime import datetime, timedelta
|
|
|
from typing import Any, Dict, List, Optional
|
|
|
@@ -36,6 +37,10 @@ class DataProvider:
|
|
|
self._headers: Dict[str, str] = {"Content-Type": "application/json"}
|
|
|
self._db_engine = None
|
|
|
|
|
|
+ # API 限速:防止多线程并发请求导致竞争
|
|
|
+ self._api_lock = threading.Lock()
|
|
|
+ self._last_request_time = 0.0
|
|
|
+
|
|
|
self._login()
|
|
|
|
|
|
# ======================== Token ========================
|
|
|
@@ -77,7 +82,13 @@ class DataProvider:
|
|
|
raise
|
|
|
|
|
|
def _request(self, method: str, url: str, **kwargs) -> Optional[requests.Response]:
|
|
|
- """统一请求封装,自带 Token。"""
|
|
|
+ """统一请求封装,自带 Token,限速防并发竞争。"""
|
|
|
+ with self._api_lock:
|
|
|
+ elapsed = time.time() - self._last_request_time
|
|
|
+ if elapsed < 0.5:
|
|
|
+ time.sleep(0.5 - elapsed)
|
|
|
+ self._last_request_time = time.time()
|
|
|
+
|
|
|
kwargs.setdefault("headers", self._headers)
|
|
|
kwargs.setdefault("timeout", 60)
|
|
|
try:
|