1
2You may use the signal package if you are running on UNIX:
3
4import signal
5
6# Register an handler for the timeout
7def handler(signum, frame):
8 print("Forever is over!")
9 raise Exception("end of time")
10
11
12# This function *may* run for an indetermined time...
13def loop_forever():
14 import time
15 while 1:
16 print("sec")
17 time.sleep(1)
18
19
20
21# Register the signal function handler
22signal.signal(signal.SIGALRM, handler)
23
24
25# Define a timeout for your function
26signal.alarm(10)
270
28
29try:
30 loop_forever()
31except Exception, exc:
32 print(exc)
1#import time module:
2import time
3#module has various attributes:
4dir(time)
5[..., 'localtime', 'mktime', 'sleep', 'sleep_ms', 'sleep_us', 'ticks_add', 'ticks_cpu', 'ticks_diff', 'ticks_ms', 'ticks_us', 'time']
6
7#default expression is in seconds with zero being start of runtime
8secFromStart = time.time()