Below there is some fully functioning code.
I am planning to execute this code through command line, however I would like it to end after 60 seconds.
Does an
Try this out:
import os
import time
from datetime import datetime
from threading import Timer
def exitfunc():
print "Exit Time", datetime.now()
os._exit(0)
Timer(5, exitfunc).start() # exit in 5 seconds
while True: # infinite loop, replace it with your code that you want to interrupt
print "Current Time", datetime.now()
time.sleep(1)
There are some more examples in this StackOverflow question: Executing periodic actions in Python
I think the use of os._exit(0)
is discouraged, but I'm not sure. Something about this doesn't feel kosher. It works, though.