python - 操作时限

标签 python maya

有没有办法在 python 中限制时间,例如:

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

最佳答案

如果您使用的是 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)。您当然可以调整时间限制。

关于python - 操作时限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44952761/

相关文章:

python:如何检测我的线程何时成为孤儿?

python - 如何反序列化由 to_xml() 在 google appengine 中创建的 xml

python - 在 Jupyter Notebook 中使用 %%time 在连续单元格中更改变量范围

python - 有没有一种方法可以在不影响代码的情况下缩进 Python

python - 从目标 Maya Python API 查找 Blendshape

python - 如何在没有 UI 的情况下打开 Maya?

Python 向量类

python - Material 和纹理更改脚本(之前询问过)

python - 通过自省(introspection)打印 ctypes "Structure"的所有字段

python - 无法弄清楚 python 查询。