问题描述
限时送ChatGPT账号..Mathematica 具有 CheckAbort
函数,它允许捕获和处理用户生成的和程序化的 Abort
s.但是它不允许捕获由TimeConstrained
和MemoryConstrained
等函数产生的中断:
Mathematica has the CheckAbort
function which allows to catch and handle user-generated and programmatic Abort
s. But it does not allow to catch interrupts generated by such functions as TimeConstrained
and MemoryConstrained
:
TimeConstrained[CheckAbort[Pause[100], Print["From CheckAbort"]], 1]
(不打印From CheckAbort"
).
有没有办法在 Mathematica 中捕获此类中断?
Is there a way to catch such interrupts in Mathematica?
我知道 TimeConstrained
和 MemoryConstrained
的第三个参数允许在中断的情况下评估一些代码,但这种方式是不是我需要的:我需要一种方法来完全在我的函数内部处理此类中断,从而允许用户不关心其内部结构.
I do know that third argument of TimeConstrained
and MemoryConstrained
allows to evaluate some code in the case of interrupt but this way is not what I need: I need a way to handle such interrupts entirely inside of my function allowing a user do not care of its internals.
附言我需要这个的原因是我有一个创建 MathLink
对象的函数,这些对象在任何中断或中止的情况下都必须关闭,但在其他情况下则不能.
P.S. The reason why I need this is that I have a function that creates MathLink
objects which must be closed in the case of any interrupts or aborts but not in other cases.
推荐答案
此构造以未记录的形式提供.
The construct for this is available in undocumented form.
Internal`WithLocalSettings[
preprocessing,
code,
postprocessing]
将导致在从中止或各种类型的跳转返回之前进行后处理.
will cause postprocessing to take place before returning from aborts or various types of jumps.
另见:
Mathematica 中的可靠清理
使用 mathematica 导入大文件/数组
丹尼尔·利希布劳
这篇关于如何捕获 TimeConstrained 产生的中断?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论