由 JLink 或 UseFrontEnd 生成的 Uncaught Throw

发布于 2024-12-08 03:01:35 字数 1203 浏览 3 评论 0原文

此示例例程在内核窗口中生成两条 Throw::nocatch 警告消息。可以以某种方式处理它们吗?

该示例包含在 C:\Temp 中创建的文件“test.m”中的以下代码:

Needs["JLink`"];
$FrontEndLaunchCommand = "Mathematica.exe";
UseFrontEnd[NotebookWrite[CreateDocument[], "Testing"]];

然后粘贴这些命令并在 Windows 命令提示符下运行:

PATH = C:\Program Files\Wolfram Research\Mathematica\8.0\;%PATH%
start MathKernel -noprompt -initfile "C:\Temp\test.m"

在此处输入图像描述

附录

使用 UseFrontEnd 而不是UsingFrontEnd 的原因是,可能需要交互式前端来保留通常以交互方式运行的笔记本的输出和消息。例如,对 C:\Temp\test.m 进行如下修改:

Needs["JLink`"];
$FrontEndLaunchCommand="Mathematica.exe";
UseFrontEnd[
nb = NotebookOpen["C:\\Temp\\run.nb"];
SelectionMove[nb, Next, Cell];
SelectionEvaluate[nb];
];
Pause[10];
CloseFrontEnd[];

以及使用包含以下内容的单个单元格创建的笔记本 C:\Temp\run.nb:

x1 = 0; While[x1 < 1000000,
 If[Mod[x1, 100000] == 0,
  Print["x1=" <> ToString[x1]]]; x1++];
NotebookSave[EvaluationNotebook[]];
NotebookClose[EvaluationNotebook[]];

此代码从 Windows 命令提示符启动,将以交互方式运行并保存其内容输出。使用UsingFrontEnd 或MathKernel 脚本“C:\Temp\test.m”无法实现此目的。

This example routine generates two Throw::nocatch warning messages in the kernel window. Can they be handled somehow?

The example consists of this code in a file "test.m" created in C:\Temp:

Needs["JLink`"];
$FrontEndLaunchCommand = "Mathematica.exe";
UseFrontEnd[NotebookWrite[CreateDocument[], "Testing"]];

Then these commands pasted and run at the Windows Command Prompt:

PATH = C:\Program Files\Wolfram Research\Mathematica\8.0\;%PATH%
start MathKernel -noprompt -initfile "C:\Temp\test.m"

enter image description here

Addendum

The reason for using UseFrontEnd as opposed to UsingFrontEnd is that an interactive front end may be required to preserve output and messages from notebooks that are usually run interactively. For example, with C:\Temp\test.m modified like so:

Needs["JLink`"];
$FrontEndLaunchCommand="Mathematica.exe";
UseFrontEnd[
nb = NotebookOpen["C:\\Temp\\run.nb"];
SelectionMove[nb, Next, Cell];
SelectionEvaluate[nb];
];
Pause[10];
CloseFrontEnd[];

and a notebook C:\Temp\run.nb created with a single cell containing:

x1 = 0; While[x1 < 1000000,
 If[Mod[x1, 100000] == 0,
  Print["x1=" <> ToString[x1]]]; x1++];
NotebookSave[EvaluationNotebook[]];
NotebookClose[EvaluationNotebook[]];

this code, launched from a Windows Command Prompt, will run interactively and save its output. This is not possible to achieve using UsingFrontEnd or MathKernel -script "C:\Temp\test.m".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苄①跕圉湢 2024-12-15 03:01:35

在初始化期间,内核代码处于防止中止的模式。

Throw/Catch 是通过 Abort 实现的,因此它们在初始化期间不起作用。

显示问题的一个简单示例是将其放入 test.m 文件中:

Catch[Throw[test]];

同样,TimeConstrained、MemoryConstrained、Break、Trace 系列、Abort 等函数以及依赖于它的函数(如某些数据小包)也会出现这样的问题在初始化期间。

解决您的问题的一个可能的解决方案可能是考虑 -script 选项:

math.exe -script test.m

另外,请注意,在版本 8 中,有一个名为UsingFrontEnd 的记录函数,它执行 UseFrontEnd 所做的操作,但是是自动配置的,因此:

Needs["JLink`"];
UsingFrontEnd[NotebookWrite[CreateDocument[], "Testing"]];

应该是您所需要的在你的 test.m 文件中。

另请参阅:Mathematica 脚本

附录

一种可能的解决方案使用-script和UsingFrontEnd是使用'run.m脚本
包括在下面。这确实需要在内核配置选项中设置“测试”内核(基本上是“本地”内核设置的克隆)。

该脚本包括两个实用函数:NotebookEvaluatingQ 和 NotebookPauseForEvaluation,它们帮助脚本等待客户端笔记本完成评估后再保存。这种方法的优点是所有评估控制代码都在“run.m”脚本中,因此客户端笔记本末尾不需要有 NotebookSave[EvaluationNotebook[]] 语句。

NotebookPauseForEvaluation[nb_] := Module[{},While[NotebookEvaluatingQ[nb],Pause[.25]]]

NotebookEvaluatingQ[nb_]:=Module[{},
SelectionMove[nb,All,Notebook];
Or@@Map["Evaluating"/.#&,Developer`CellInformation[nb]]
]

UsingFrontEnd[
nb = NotebookOpen["c:\\users\\arnoudb\\run.nb"];
SetOptions[nb,Evaluator->"Test"];
SelectionMove[nb,All,Notebook];
SelectionEvaluate[nb];
NotebookPauseForEvaluation[nb];
NotebookSave[nb];
]

我希望这对您有所帮助。它可以使用更多的改进,例如将笔记本的内核重置为其原始状态并在保存后关闭笔记本,
但这段代码应该适用于这个特定的目的。

顺便说一句,我尝试了另一种方法,使用以下方法:

UsingFrontEnd[ NotebookEvaluate[ "c:\\users\\arnoudb\\run.nb", InsertResults->True ] ]

但这是将内核终端会话踢入对话模式,这似乎是一个错误
对我来说(如果这是一个有效的问题,我会检查并报告此问题)。

During the initialization, the kernel code is in a mode which prevents aborts.

Throw/Catch are implemented with Abort, therefore they do not work during initialization.

A simple example that shows the problem is to put this in your test.m file:

Catch[Throw[test]];

Similarly, functions like TimeConstrained, MemoryConstrained, Break, the Trace family, Abort and those that depend upon it (like certain data paclets) will have problems like this during initialization.

A possible solution to your problem might be to consider the -script option:

math.exe -script test.m

Also, note that in version 8 there is a documented function called UsingFrontEnd, which does what UseFrontEnd did, but is auto-configured, so this:

Needs["JLink`"];
UsingFrontEnd[NotebookWrite[CreateDocument[], "Testing"]];

should be all you need in your test.m file.

See also: Mathematica Scripts

Addendum

One possible solution to use the -script and UsingFrontEnd is to use the 'run.m script
included below. This does require setting up a 'Test' kernel in the kernel configuration options (basically a clone of the 'Local' kernel settings).

The script includes two utility functions, NotebookEvaluatingQ and NotebookPauseForEvaluation, which help the script to wait for the client notebook to finish evaluating before saving it. The upside of this approach is that all the evaluation control code is in the 'run.m' script, so the client notebook does not need to have a NotebookSave[EvaluationNotebook[]] statement at the end.

NotebookPauseForEvaluation[nb_] := Module[{},While[NotebookEvaluatingQ[nb],Pause[.25]]]

NotebookEvaluatingQ[nb_]:=Module[{},
SelectionMove[nb,All,Notebook];
Or@@Map["Evaluating"/.#&,Developer`CellInformation[nb]]
]

UsingFrontEnd[
nb = NotebookOpen["c:\\users\\arnoudb\\run.nb"];
SetOptions[nb,Evaluator->"Test"];
SelectionMove[nb,All,Notebook];
SelectionEvaluate[nb];
NotebookPauseForEvaluation[nb];
NotebookSave[nb];
]

I hope this is useful in some way to you. It could use a few more improvements like resetting the notebook's kernel to its original and closing the notebook after saving it,
but this code should work for this particular purpose.

On a side note, I tried one other approach, using this:

UsingFrontEnd[ NotebookEvaluate[ "c:\\users\\arnoudb\\run.nb", InsertResults->True ] ]

But this is kicking the kernel terminal session into a dialog mode, which seems like a bug
to me (I'll check into this and get this reported if this is a valid issue).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文