操作时间限制

时间:2017-07-06 15:09:40

标签: python maya

有没有办法限制python中的oparations,例如:

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

1 个答案:

答案 0 :(得分:1)

如果您使用的是Mac或基于Unix的系统,则可以使用signal.SIGALRM强制超时耗时的功能,因此您的代码将如下所示:

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

基本上,您创建了一个自定义的异常,该异常是在时间限制结束后(即发送SIGALRM)引发的。你当然可以调整时间限制。