0

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)
Kiran
  • 167
  • 1
  • 9
  • you are unable to timeout your function because your call to the Broker is blocking (it wasn't designed to work with async). you have several approaches here, the best would be to get/make a Broker that works with async, you can try signals as suggested in the answer below (I understand it's somewhat problematic on windows), or you can use subprocees. if you do your broker call in a subprocess, then you can set a timeout that will work – Nullman Mar 06 '23 at 23:09

1 Answers1

0

This can be accomplished via the time module.

import time


 async def FMinData():
    print(datetime.now())
    try:
        start = time.perf_counter() # more accurate than time.time()
        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.
                # Check if time is greater than 20 seconds (time.perf_counter()-start measures time elapsed)
                if time.perf_counter()-start >= 20:
                    raise TimeoutError("Program took too long to run")
            except:
                os._exit(0)                        
    print(datetime.now())
    except asyncio.TimeoutError:        
        os._exit(0)
    except:        
        os._exit(0)
    finally:        
        os._exit(0)
Jason Grace
  • 321
  • 11
  • what if the function got stuck at receiving the data from the API itself... It couldn't raise the exception, as it's still waiting for the data from the API!.. Actually, I need to terminate the script/function if the time is more than 20 sec for whatever reason. – Kiran Mar 06 '23 at 21:57
  • @Kiran Take a look at [this](https://stackoverflow.com/a/494273/19916174) answer from a different stack overflow user, and let me know if it answers your question. – Jason Grace Mar 06 '23 at 22:04
  • I checked the above thread, it seems as most of them are with signal which windows weirdly supports, the rest of the external lib also not working when I backtest on windows – Kiran Mar 07 '23 at 04:56