Replies: 2 comments 6 replies
-
The good first-pass answer for anything like this would be to figure out what folks are typically doing for (Since we're broadly API compatible) |
Beta Was this translation helpful? Give feedback.
-
Following @StephenBrown2's reply I now implemented this using tenacity. For the record, this is how the updated function currently looks like (I removed the docstring for this preview): @tenacity.retry(stop=tenacity.stop_after_attempt(5), wait=tenacity.wait_fixed(2))
async def call_api(client: httpx.AsyncClient, api_call: str) -> dict[str, Any]:
response: httpx.Response = await client.get(api_call)
response.raise_for_status()
return response.json() To exit the program when all retries failed (again, code is truncated here): async with httpx.AsyncClient(timeout=None) as client:
# Building asyncio task list here, with each task calling call_api() shown above...
try:
api_responses: list[dict[str, Any]] = await asyncio.gather(*tasks)
except tenacity.RetryError:
sys.exit("API appears unresponsive, please try again later.") Overall, the code is simplified and more readable. I didn't test this a lot, but it seems to work well. If you have any suggestions for improvements, please let me know :-) Maybe consider adding a section about this to the docs, perhaps with a recommendation for tenacity? And as someone who is not familar with |
Beta Was this translation helpful? Give feedback.
-
Hi everyone! Thanks for this wonderful tool. I was hoping to get some feedback and suggestions for something I wrote. I've build a simple tool that turns URLs in Markdown files into snapshots from archive.org. I use httpx's AsyncClient to call the API. The problem is that the API can be slow or unresponsive, in which case it returns a 503 reponse. I've tried to work around this by letting the individual task (each task is a call to the API) wait for five seconds and then retry for a maximum of five times (here is the code in context):
It kinda works, but I'm really unsure about the 'proper' way of doing this. Would be happy for any feedback and suggestions!
Beta Was this translation helpful? Give feedback.
All reactions