Reputation: 974
I m in a situation where I have a func that calls the 3rd party API using the native lib of that company. Usually, the response time is excellent, like 1-2 secs but sometimes, the func would take 30-50 seconds to get the response. I want to make the func work in way that if it takes longer than the 5 seconds, then I cancel the call and retry the func. I have tried several combo's of tenacity and concurrent futures but nothing really showing a potential outcome that I need.
Note: The 3rd party when it takes long time to respond doesn't show as hung, that's another thing, so we need to find something that'll simply wait 5 seconds and kil it and retry.
Here is what I tried:
@retry(wait=wait_random_exponential(min=3, max=5), stop=stop_after_attempt(3), retry_error_callback=lambda x: logger.info("Retrying getting vectors..."))
def get_vectors(namespace):
def query():
####SOME CODE for 3rd party API###
return some_output
with concurrent.futures.ThreadPoolExecutor() as executor:
future = executor.submit(query)
try:
return future.result(timeout=5)
except concurrent.futures.TimeoutError:
logger.info('5 secs timeout happened...')
future.cancel()
raise Exception("Function took too long to complete. Retrying...")
Upvotes: 1
Views: 497