0
$\begingroup$

My problem is that the second notebook file opens but waits for the first notebook to be evaluated. I want to execute both file1.nb and file2.nb at the same time. Here is my code. any idea?

NotebookPauseForEvaluation[nb_] := Module[{},
    While[NotebookEvaluatingQ[nb], Pause[1]]
];

NotebookEvaluatingQ[nb_] := Module[{},
    SelectionMove[nb, All, Notebook];
    Or @@ Map["Evaluating" /. # &, Developer`CellInformation[nb]]
];

UsingFrontEnd[
    nb1 = NotebookOpen["./file1.nb"];
    SetOptions[nb1, Evaluator -> "RemoteKernel1"];
    SelectionMove[nb1, All, Notebook];
    SelectionEvaluate[nb1];
    NotebookPauseForEvaluation[nb1];
    NotebookSave[nb1];
    
    nb2 = NotebookOpen["./file2.nb"];
    SetOptions[nb2, Evaluator -> "RemoteKernel2"];
    SelectionMove[nb2, All, Notebook];
    SelectionEvaluate[nb2];
    NotebookPauseForEvaluation[nb2];
    NotebookSave[nb2];
    
  ];

$\endgroup$
7
  • $\begingroup$ Before executing the notebooks, assign different kernels to the two notebooks, using the menu option Evaluation > Notebook's Kernel. Note, however, that your license may limit the number of kernels running simultaneously. $\endgroup$
    – bbgodfrey
    Nov 13, 2021 at 17:47
  • $\begingroup$ thanks for comment. i did it before. $\endgroup$ Nov 13, 2021 at 17:49
  • $\begingroup$ Another thing you usually should do is assure that the default Contexts for the two notebooks are different, so that they do not interfere with each other. I usually use Evaluation > Notebook's Default Context > Unique to This Notebook. $\endgroup$
    – bbgodfrey
    Nov 13, 2021 at 17:54
  • $\begingroup$ Since this code runs in a hpc cluster, i don't have access to compute nodes to do this, how can i do this programmatic? $\endgroup$ Nov 13, 2021 at 18:07
  • $\begingroup$ Good question for which I do not have an answer. If you can get to the Options Inspector you can set many useful defaults, including Unique to This Notebook. $\endgroup$
    – bbgodfrey
    Nov 13, 2021 at 18:14

0

Your Answer

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