9

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+.

ahle6481
  • 565
  • 2
  • 14

1 Answers1

6

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.

Rolf Mertig
  • 17,172
  • 1
  • 45
  • 76
  • 2
    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. – Leonid Shifrin Oct 03 '16 at 19:11
  • @LeonidShifrin That it does not work for you is a bug in the macOS version. On Windows it works in 11.0.1. – Rolf Mertig Oct 03 '16 at 20:06
  • Interesting. I didn't know this was possible. Can't check right now on Windows, but thanks for the info. – Leonid Shifrin Oct 03 '16 at 20:17
  • 1
    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? – Leonid Shifrin Oct 03 '16 at 20:18
  • @LeonidShifrin The FrontEnd is still an interesting piece of sofware. Full of surprises. – Rolf Mertig Oct 03 '16 at 21:58
  • 3
    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. – Leonid Shifrin Oct 03 '16 at 22:33
  • @LeonidShifrin yep it is tricky: 98882. I don't know why there are tools missing to handle basic activities in FE. – Kuba Oct 04 '16 at 07:28
  • @Kuba Somehow I forgot about that one, even though there is my vote there for both the question and your answer. Thanks for reminding about it. – Leonid Shifrin Oct 04 '16 at 15:08