Answers for "timeout python"

2

python timeout

You may use the signal package if you are running on UNIX:

import signal

# Register an handler for the timeout
def handler(signum, frame):
  print("Forever is over!")
       raise Exception("end of time")
   

# This function *may* run for an indetermined time...
def loop_forever():
     import time
     while 1:
         print("sec")
         time.sleep(1)
         
         

# Register the signal function handler
signal.signal(signal.SIGALRM, handler)


# Define a timeout for your function
signal.alarm(10)
0

try:
    loop_forever()
except Exception, exc:
    print(exc)
Posted by: Guest on February-26-2021
2

time python

#import time module:
import time
#module has various attributes: 
dir(time)
[..., 'localtime', 'mktime', 'sleep', 'sleep_ms', 'sleep_us', 'ticks_add', 'ticks_cpu', 'ticks_diff', 'ticks_ms', 'ticks_us', 'time']

#default expression is in seconds with zero being start of runtime
secFromStart = time.time()
Posted by: Guest on December-28-2020

Python Answers by Framework

Browse Popular Code Answers by Language