Group Abstract Group Abstract

Message Boards Message Boards

4
|
7.2K Views
|
3 Replies
|
9 Total Likes
View groups...
Share
Share this post:

Run NotebookEvaluate on a "slave" kernel controlled through MathLink?

Posted 9 years ago

I am evaluating commands on a "slave kernel" which I control from a "master kernel" through MathLink in a manner similar to this:

The slave kernel is of a different (older) version than the master.

I want to evaluate

UsingFrontEnd[NotebookEvaluate[..., InsertResults -> True]]

on the slave, but it doesn't work: it simply hangs. If I run the same in a terminal, i.e. with a kernel not controlled through MathLink and $ParentLink not set, it works. UsingFrontEnd operations which do require a front end but do not require that front end to call back to the kernel also work.

I am not surprised that this doesn't work, I imagine the subsidiary front end gets into trouble when it tries to call back to its kernel, as the parent link of that kernel is taken. I don't understand the details.

But is there an easy solution to this? Or should I better give up?

Why do I want this? I want to process some documentation notebooks in a the oldest version of Mathematica that the package is compatible with. I am trying to automate all this and I have a function which can launch a subsidiary Mathematica process of the requested version and evaluate various things with it.

POSTED BY: Szabolcs Horvát
3 Replies

I've struggled with NotebookEvaluate[] as well when run from a subkernel. One of my workarounds for simpler notebooks is to write my own "NotebookEvaluate" function:

1) read the raw form of the notebook using Get

2) replace every Input cell type with a sequence of Input and Output cells, where the Output cell has gone from its BoxForm, ToExpression, and back to BoxForm.

evaluateNotebook[loc_String] := Module[{test,nb},
  UsingFrontEnd[
    test=Get[loc];
    nb=CreateDocument[test/.Cell[BoxData[x___],"Input",y___] :> Sequence[Cell[BoxData[x],"Input",y],Cell[BoxData[ToBoxes[ToExpression[BoxData[x]]]],"Output"]]];
    NotebookSave[nb,loc];
    NotebookClose[nb];
  ]
]

This will not overwrite older Output or warning messages, but you can add some rules to remove those, too, using empty sequences:

  (*rule to remove old output cells*)
  Cell[BoxData[___], "Output", ___] -> Sequence[]; 

  (*rules to remove old message cells*)
  Cell[BoxData[___], "Message", ___] -> Sequence[]; 

I'm not sure how robust this approach is, i.e. I haven't tested how it deals with cells that are commented out, but it does a good job for how short it is. I'm open to any feedback on improving it.

POSTED BY: Kevin Daily

You are right. At this point my vague impression is that it is generally safer to manipulate notebook expressions using the kernel rather than the Front End, even though the Front End way tends to be much more convenient.

POSTED BY: Szabolcs Horvát
POSTED BY: Szabolcs Horvát
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard