# UsingFrontEnd with compute kernels

I am trying to have each compute kernel in a Mathematica session evaluate a notebook with NotebookEvaluate. Here is a sketch of my code:

LaunchKernels[]

computeNotebooks = Map[ FileNameJoin[{NotebookDirectory[],#}]&,
{"ComputeNotebook1.nb","ComputeNotebook2.nb","ComputeNotebook3.nb"} ]

ParallelMap[ UsingFrontEnd[NotebookEvaluate[#,InsertResults->True]]&, computeNotebooks ]


The notebooks are located in the same directory as the driver notebook which contains the commands above.

With both Mathematica 10.4 and 11 the compute kernels seem to launch a front end but the ParallelMap command then never completes. Aborting the evaluation does not work either.

Also note that running the evaluation on the controller kernel works without problems:

Map[ UsingFrontEnd [NotebookEvaluate[#,InsertResults->True]]&, computeNotebooks ]


Both UsingFrontEnd and ParallelMap are using the same mechanism for tracking the parent process, so they conflict with each other. This is a known issue. In the meantime, there is a workaround: a separate evaluator for each notebook. For instance, here is a set up I used recently,

(* Set up the known evaluators *)
$processorCount = 4; SetOptions[$FrontEnd,
EvaluatorNames -> Join[{"Local" -> {"AutoStartOnLaunch" -> True}},
Table["Kernel" <> ToString[i] -> {"AutoStartOnLaunch" -> False},
{i, $processorCount}]]] MapIndexed[ Function[{file, k}, Module[{nb}, nb = NotebookOpen[file]; SetOptions[nb, Evaluator -> "Kernel" <> ToString[Mod[First[k],$processorCount, 1]]
];
FrontEndTokenExecute[nb, "SelectAll"];
FrontEndTokenExecute[nb, "SelectionOpenAllGroups"];
FrontEndTokenExecute[nb, "EvaluateCells"];
]
],
files
];


The disadvantage of this method is that you cannot queue up the notebooks, if you have a lot of them, which would allow the load to be better balanced between the kernels.