一个django模板标记,用于强制模板内的呈现时间限制。
django-timelimit的Python项目详细描述
django timelimit允许您使用 在呈现时间上强制上限的超时。当模板超过 这一次,将呈现替代片段:
{% load timelimit %} {% timelimit 0.05 %} {# perform a potentially very slow operation #} <p>No timeout occurred</p> {% sleep 0.5 %} {% else %} <p>Sorry, couldn't render the fragment in time.</p> {% endtimelimit %}
安装
$ pip install django-timelimit
可重入
django timelimit是完全可重入的,这意味着您可以嵌套 超时:
{% load timelimit %} {% timelimit 0.10 %} {# perform a potentially very slow operation #} {% timelimit 0.05 %} {# perform another potentially slow operation #} {% else %} <p>Sorry, couldn't render the nested fragment in time.</p> {% endtimelimit %} <p>No timeout occurred</p> {% else %} <p>Sorry, couldn't render the fragment in time.</p> {% endtimelimit %}
嵌套超时允许较大的外部超时包含较小的超时。如果 内部超时大于外部超时,它被视为无操作。
配额
也支持中断CoW配额(在中断CoW 0.7中引入)。 允许您在不同的时间限制之间共享一个时间分配 标签:
{% load timelimit %} {% for foo in bar %} {% timelimit quota_var %} ... {% else %} ... {% endtimelimit %} {% endif %}
这在不希望总渲染时间增加的循环中很有用 循环的迭代次数。
注意事项
django timelimit基于中断cow,因此它也有其局限性。 interruptingcow使用signal(SIGALRM)让操作系统中断 程序执行,意思是:
- python信号处理程序只应用于主线程,因此不能使用 从其他线程(这也意味着您不能在多线程中使用它 Web服务器,甚至是使用后台线程处理请求的服务器 (Gunicorn的作品很棒)。
- 您不能在使用SIGALRM本身的程序中使用这个 包括某些探查器)