I am trying to run a code which tries to fetch data from an API:
client.quotes(instrument_tokens=inst_tokens, quote_type="", isIndex=False)
However, sometimes this code keeps running indefinitely without returning any data and I need to interrupt the code using the keyboard and run my code again. I would like to handle this in my code wherein if it takes more than 10 seconds to fetch the data, I retry fetching this data after say, 10 seconds. I have tried using threading, something like
while True:
thread = threading.Thread(target=run_with_timeout(inst_tokens))
thread.start()
thread.join(timeout=10)
if thread.is_alive():
print("Execution of quote API took more than 10 seconds. Retrying in 15 seconds...", str(datetime.datetime.now()))
thread.join() # Ensure the thread is terminated before retrying
time.sleep(retry_delay) # Wait before retrying
continue # Restart the loop
break # Exit the loop if code ran within time limit
where run_with_timeout
is essentially the client.quotes
function. Yet this doesn't seem to work and my code still gets stuck running indefinitely. Can anyone please help me with this issue.