9
$\begingroup$

Is there a command to abort all evaluations in a Mathematica Kernel? Basically I want something like:

 somecode;
 AbortAll[];
 morecode;

Where the AbortAll[] command stops all running and queued evaluations. I cannot find a command for this, only drop-down menu options or pressing alt+.

$\endgroup$
6
$\begingroup$

Using Mathematica 11.0.1 on Windows 10 or Linux (openSuse) this does work:

 abortAll[] := FrontEndTokenExecute["EvaluatorAbort"];
        Do[Print[i]; If[i > 2, abortAll[]]; Pause[.2]; , {i, 10}]

        Print["*"]; abortAll[]; Print["**"];

works on Linux.

On Windows it depends on the setting of CurrentValue[$FrontEnd, PrintAction]: For the default CurrentValue[$FrontEnd, PrintAction] = "PrintToNotebook"; the second print cell with "**" shows up. However, for CurrentValue[$FrontEnd, PrintAction] = "PrintToConsole"; (as I have it normally, even though I do not understand why it is called "PrintToConsole", since it prints to a Messages window) there is only one Print executed.

enter image description here

On Linux it just works consistently.

On macOS 10.12 using Mathematica 11.0.1 always both Print's are evaluated.

If anybody cares to use this on the cloud, then:

If[$Notebooks&&!$CloudEvaluation,CurrentValue[$FrontEnd, PrintAction] = "PrintToConsole"]; 
abortAll[] := If[$Notebooks&&!$CloudEvaluation, FrontEndTokenExecute["EvaluatorAbort"],Abort[]]; 
Print["*"]; abortAll[]; Print["**"]; 

which leaves me puzzled why $Notebooks is True when $CloudEvaluation is True.

| improve this answer | |
$\endgroup$
  • 2
    $\begingroup$ Doesn't work for me: Print["*"];abortAll[];Print["**"]; still prints both "*" and " ** " (when on separate lines, of course). Moreover, my understanding is that it is impossible to do when lines are entered one by one in a cell and not wrapped in some head (like head[a;b;c]), since every such "top level" line is sent to the kernel by FE as a separate evaluation. $\endgroup$ – Leonid Shifrin Oct 3 '16 at 19:11
  • $\begingroup$ @LeonidShifrin That it does not work for you is a bug in the macOS version. On Windows it works in 11.0.1. $\endgroup$ – Rolf Mertig Oct 3 '16 at 20:06
  • $\begingroup$ Interesting. I didn't know this was possible. Can't check right now on Windows, but thanks for the info. $\endgroup$ – Leonid Shifrin Oct 3 '16 at 20:17
  • 1
    $\begingroup$ Note however that placing things in Do is different from what was originally posted and from what I was considering. Now that there is a wrapper around the code, it is all sent to the kernel as a single evaluation, so I am not so surprised that it works in this case. So, does the original code in the question (or my version) also abort with your construct? $\endgroup$ – Leonid Shifrin Oct 3 '16 at 20:18
  • 3
    $\begingroup$ It also really matters whether the statements are in one line, or in separate lines, if they are "top-level" in the cell (that is, not wrapped in something). FrontEnd sends separate "top-level" lines as separate evaluations to the kernel, and this is why Abort in one does not affect the others. If all statements are in one line, however, then that line is sent to the kernel as a single evaluation, and then Abort in the middle statement will indeed affect those after it in that line. $\endgroup$ – Leonid Shifrin Oct 3 '16 at 22:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.