CheckAbort inside MathLink functions?

醉酒当歌 提交于 2019-12-08 15:11:03

问题


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?


回答1:


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 with CheckAbort, you will need to check the return value and generate another abort, for example:

 If[LinkRead[link] == $Aborted, Abort[]]

This works if you know that the other end of the link returns $Aborted in case it is aborted.



来源:https://stackoverflow.com/questions/5636135/checkabort-inside-mathlink-functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!