Work limit

is there a way to limit the running time of oparations in python like:

try:
    cmds.file( file, o=1, pmt=0 )
except:
    print "Sorry, run out of time"
    pass

      

+3


source to share


1 answer


If you are on a Mac or Unix based system, you can use signal.SIGALRM

to force shutdown times that take too long, so your code will look like this:

import signal

class TimeoutException(Exception):   # custom exception
    pass

def timeout_handler(signum, frame):   # raises exception when signal sent
    raise TimeoutException

# Makes it so that when SIGALRM signal sent, it calls the function timeout_handler, which raises your exception
signal.signal(signal.SIGALRM, timeout_handler)

# Start the timer. Once 5 seconds are over, a SIGALRM signal is sent.
signal.alarm(5)
try:
    cmds.file( file, o=1, pmt=0 )
except TimeoutException:
    print "Sorry, run out of time" # you don't need pass because that in the exception definition

      



Basically, you are throwing a custom exception that is thrown when after the expiration date (i.e. dispatched SIGALRM

). You can, of course, adjust the time limit.

+1


source







All Articles