Search code examples
wolfram-mathematicamathematica-frontend

Self-restarting MathKernel - is it possible in Mathematica?


This question comes from the recent question "Correct way to cap Mathematica memory use?"

I wonder, is it possible to programmatically restart MathKernel keeping the current FrontEnd process connected to new MathKernel process and evaluating some code in new MathKernel session? I mean a "transparent" restart which allows a user to continue working with the FrontEnd while having new fresh MathKernel process with some code from the previous kernel evaluated/evaluating in it?

The motivation for the question is to have a way to automatize restarting of MathKernel when it takes too much memory without breaking the computation. In other words, the computation should be automatically continued in new MathKernel process without interaction with the user (but keeping the ability for user to interact with the Mathematica as it was originally). The details on what code should be evaluated in new kernel are of course specific for each computational task. I am looking for a general solution how to automatically continue the computation.


Solution

  • From a comment by Arnoud Buzing yesterday, on Stack Exchange Mathematica chat, quoting entirely:

    In a notebook, if you have multiple cells you can put Quit in a cell by itself and set this option:

    SetOptions[$FrontEnd, "ClearEvaluationQueueOnKernelQuit" -> False]
    

    Then if you have a cell above it and below it and select all three and evaluate, the kernel will Quit but the frontend evaluation queue will continue (and restart the kernel for the last cell).

    -- Arnoud Buzing