timeout - 如何优雅地终止无响应的 tcl 脚本?

标签 timeout tcl

假设我有一个 tcl 脚本,通常应在不到一分钟的时间内执行 - 我如何确保脚本的执行时间永远不会超过“x”分钟/秒,如果确实如此,则脚本应该只是被阻止。

例如,如果脚本花费了超过 100 秒,那么我应该能够自动将控制权切换到清理函数,该函数将优雅地结束脚本,以便我拥有迄今为止运行的脚本的所有数据,但是我还确保它不会花费太长时间或无限地卡住。

我不确定这是否可以在 tcl 中完成 - 欢迎任何帮助或指示。

最佳答案

您可以使用interp limit当您使用 child 口译员时。

请注意,如果您想进行一些清理以删除父插值中的限制,这将引发无法缓存的错误。

set interp [interp create]
# initialize the interp
interp eval $interp {
     source somestuff.tcl
}
# Add the limit. From now you have 60 seconds or an error will be thrown
interp limit $interp time -seconds [clock seconds] -milliseconds 60000
set errorcode [catch {interp eval {DoExpensiveStuff}} res opts]
# remove the limit so you can cleanup the mess if needed.
interp limit $interp time -seconds {}
if {$errorcode} {
    # Do some cleanup here
}
# delete the interp, or reuse it?
interp delete $interp
# And what shall be done with the error? Throw it.
return -options $opt $res

资源限制是 Tcl 的最佳选择,但它们并非万无一失。 Tcl 不能(也不会)中止 C 程序,并且有一些方法可以让 Tcl 核心做一些艰苦的工作。

关于timeout - 如何优雅地终止无响应的 tcl 脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19970188/

相关文章:

python - 提高 Tkinter 文本的 Pygments 语法突出显示速度

r - 使用 R Tcl/Tk 的简单对话

php - 读取大TXT到MYSQL超时

Javascript - 运行时优先级和队列

python - python 2.7 Tkinter tcl错误

c - 如何找到哪个 C 函数正在调用特定的 TCL 过程?

tcl 字符串替换

c# - 使用 SQL 时 DBConnection 超时

c# - 如何使用 Session.Query 设置 Nhibernate LINQ 命令超时

Spring Session with Redis - server.session.timeout 无效