Код: Выделить всё
api_keys = []
lock = asyncio.Lock()
async def fetch_data(session, url, params, delay):
await asyncio.sleep(delay)
global api_keys
global lock
while True:
if lock.locked():
print("Lock is locked, continuing...")
continue
os.environ["API_KEY"] = api_keys[0]
params["api_key"] = os.getenv("API_KEY")
async with session.get(url, params=params) as response:
if response.status == 200:
data = await response.json()
print("Remaining requests", response.headers["x-requests-remaining"])
print("Used requests", response.headers["x-requests-used"])
return data
else:
print(
f"Failed to get odds: status_code {response.status}, response body {await response.text()}".rstrip()
)
if response.status == 401:
async with lock:
if len(api_keys) > 1:
print("Using next API key in list")
api_keys.pop(0)
continue
else:
print("No more API keys available")
return None
return None
Подробнее здесь: https://stackoverflow.com/questions/784 ... chronously