is there a way to time limit the oparations in python, eg:
try:
cmds.file( file, o=1, pmt=0 )
except:
print "Sorry, run out of time"
pass
If you're on Mac or a Unix-based system, you can use signal.SIGALRM to forcibly time out functions that take too long, so your code would look like:
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's in the exception definition
Basically, you're creating a custom exception that's raised when the after the time limit is up (i.e., the SIGALRM is sent). You can of course tweak the time limit.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With