如何捕获 TimeConstrained 产生的中断?

编程入门 行业动态 更新时间:2024-10-22 22:53:49
本文介绍了如何捕获 TimeConstrained 产生的中断?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

Mathematica 具有 CheckAbort 函数,它允许捕获和处理用户生成的和程序化的 Abort s.但是它不允许捕获由TimeConstrainedMemoryConstrained 等函数产生的中断:

Mathematica has the CheckAbort function which allows to catch and handle user-generated and programmatic Aborts. 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?

我知道 TimeConstrainedMemoryConstrained 的第三个参数允许在中断的情况下评估一些代码,但这种方式是不是我需要的:我需要一种方法来完全在我的函数内部处理此类中断,从而允许用户不关心其内部结构.

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:关键词]

本文发布于:2023-04-25 07:14:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1080274.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:TimeConstrained

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!