I have a call to read a file, that sometimes takes too long to process. I decided to use a timer thread to interrupt the read. However, the watchdog routine prints the message, and then the file download continues. I understand that sys.exit() just raises an exception and have been careful not to intercept that in my code. But what about the io module?? Since execution had passed to that routine could it eat the exception? I do not want to use the os.exit because I need the parent routine to keep processing. Any suggestions?
import threading
delay_time = 30 # delay time in seconds
def watchdog():
print('Lambda expired. Exiting...')
os._exit(1)
sys.exit() # or if you want a graceful shutdown this raises an exception that can do logging, etc.
alarm = threading.Timer(delay_time, watchdog)
alarm.start()
buffer = io.BytesIO(zip_file_stream.get()["Body"].read())
alarm.cancel()