My function below is not working as intended. It is an async function, and I want it so that when the response is a 401 and the lock is not locked, it removes the first value in api_keys
. When the lock is locked, I want it to retry, as api_keys
is currently being modified. However, the output prints multiple status code 401s in a row, but my code should change the current api key when a 401 is returned and should work from there.
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
I first tried without the if lock.locked()
line, and also moving the async with lock
line around the while loop, and just under the loop, to no avail.