在 MathLink 函数中检查中止?
我刚刚发现像 LinkWrite
和 LinkRead
这样的 MathLink
函数有类似于它自己的内部 CheckAbort
的东西,可以吸收任何中止,并且不会进一步传播它们。
这可以通过 LinkRead
轻松显示:
link = LinkLaunch[First[$CommandLine] <> " -mathlink"];
LinkRead[link];
LinkWrite[link, Unevaluated[Pause[10]]];
{LinkRead[link], Print["!!"]}
评估上述代码后,按 Alt+.,您将得到以下输出:
During evaluation of In[6]:= !!
Out[9]= {ReturnPacket[$Aborted], Null}
如您所见,中止被 LinkRead
吸收。
我的问题是它破坏了我自己基于 CheckAbort
的评估流程控制。
有没有办法拦截由 LinkRead
和 LinkWrite
等函数吸收的中止?
I just found that such MathLink
functions as LinkWrite
and LinkRead
have something like its own internal CheckAbort
that absorbs any aborts, and does not propagate them further.
This can be easily shown with LinkRead
:
link = LinkLaunch[First[$CommandLine] <> " -mathlink"];
LinkRead[link];
LinkWrite[link, Unevaluated[Pause[10]]];
{LinkRead[link], Print["!!"]}
After evaluating the above code press Alt+. and you will get the following output:
During evaluation of In[6]:= !!
Out[9]= {ReturnPacket[$Aborted], Null}
As you see the abort was absorbed by LinkRead
.
My problem is that it breaks my own flow control of evaluation based on CheckAbort
.
Is there a way to intercept aborts absorbed by such functions as LinkRead
and LinkWrite
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据 MathLink 的工作方式,如果链接上没有任何内容可读取,
LinkRead
就会阻塞。如果此时尝试中止,则会通过 MathLink 消息通道将中止消息传递到链接的另一端。如果另一端的程序表现良好,它将放弃正在执行的任何操作并发送返回值(在许多情况下$Aborted
)。如果您想将中止传播到链接末尾,以便可以使用CheckAbort
捕获它,则需要检查返回值并生成另一个中止,例如:如果您知道链接的另一端会返回
$Aborted
,以防中止。The way MathLink works,
LinkRead
blocks if there is nothing to read on the link. If you try to abort at this time, an abort message is passed via MathLink message channel to the other end of the link. If the program on the other end behaves nicely, it will drop whatever it was doing and send a return value (in many cases$Aborted
). If you want to propagate the abort to your end of the link, so that you can catch it withCheckAbort
, you will need to check the return value and generate another abort, for example:This works if you know that the other end of the link returns
$Aborted
in case it is aborted.