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$
4
  • $\begingroup$ Why not Abort[]? $\endgroup$ Commented Oct 3, 2016 at 22:12
  • 3
    $\begingroup$ A duplicate old Q on StackOverflow: "How to abort evaluation of a sequence of inputs?" $\endgroup$ Commented Oct 3, 2016 at 22:14
  • 1
    $\begingroup$ @AlexeyPopkov or maybe: 98882 $\endgroup$
    – Kuba
    Commented Oct 4, 2016 at 7:27
  • $\begingroup$ This question is very old, but there is apparently still no built-in command to do this. A workaround is to use Interrupt and select "Abort Command Being Evaluated" in the dialog. $\endgroup$ Commented Aug 11, 2023 at 14:31

1 Answer 1

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.

$\endgroup$
8
  • 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$ Commented Oct 3, 2016 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$ Commented Oct 3, 2016 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$ Commented Oct 3, 2016 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$ Commented Oct 3, 2016 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$ Commented Oct 3, 2016 at 22:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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