问题
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