I tried every possible way to terminate a function/script in the middle, if it runs more than 20 sec's, but not successful. Below is the function which I am using, how to exit the program/ function to force terminate if it takes more than 20 secs
"I just want to terminate the program/script/function in the middle, if it takes more than 20 sec's and not-responding/hanging/thrownerror/still taking time to execute" - That is all
Version: python 3.10 64 bit OS: Windows 10 64 bit.
async def FMinData():
print(datetime.now())
try:
for i in range(len(strikelistlimit)):
try:
data = Broker.historical_data(strikelistlimit[i], lastBusDay,datetime.now(), 'minute',oi=False) #api Data
# Some processing of data from the api.
except:
os._exit(0)
print(datetime.now())
except asyncio.TimeoutError:
os._exit(0)
except:
os._exit(0)
finally:
os._exit(0)
Calling the function as below
if str(sys.argv[1]) == "1":
# Calling other functions
try:
asyncio.run(asyncio.wait_for(FMinData(), timeout=20))
except asyncio.TimeoutError:
os._exit(0)
os._exit(0)