3
$\begingroup$

I'd like to parallelly construct cells (possibly dynamically updating ones) and output render them on the front-end. However, I found this is not working.

For example,

Table[CellPrint[1], {2}];

will generate two cells:

1
1

But

ParallelTable[CellPrint[1], {2}];

generates the following:

(kernel 2) BoxData[1]
(kernel 1) BoxData[1]

I'm wondering what the correct way of doing this is. Thanks.

$\endgroup$
0

1 Answer 1

3
$\begingroup$

According to the Documentation page for CellPrint,

With a text-based front end, CellPrint[cell] does the same as applying Print to the contents of cell.

Via ParallelTable we are working without a FrontEnd:

ParallelTable[Print@$FrontEnd, {1}];

(kernel 4) Null

AFAIK, the absence of a FrontEnd is equivalent to a "text-based front end".

This means that the usage of CellPrint within ParallelTable isn't justified, and we should use Print instead:

ParallelTable[Print[1], {2}];

(kernel 4) 1

(kernel 3) 1

EDIT:
As Michael E2 stresses in the comment, the current behavior isn't a bug, and it is in full accord with the above-cited Documentation statement:

The docs say that Print is applied to the cell contents. The cell contents of the cell produced by CellPrint[1] are BoxData["1"] (look at Cell > Show Expression in a regular kernel/FE). – Michael E2


I'd like to parallelly construct cells (possibly dynamically updating ones) and output render them on the front-end.

If you really need to print cells into your evaluation Notebook from parallel subkernels, you need them to have access to your interactive FrontEnd. By default, they don't have access to this FrontEnd. It may be possible however to access it via MathLink, as Oleksandr R. seemingly claims here and here. It is worth creating a separate question on it.

It is also possible to develop your own parallelization framework as I did it here.

But if you need only dynamically update a cell in your Notebook, you can make a Cell with a Dynamic expression showing the current value of a variable shared between all parallel subkernels (use SetSharedVariable to create it).

$\endgroup$
4
  • $\begingroup$ I disagree that it is a bug. It has always seemed clear that the subkernels are connected to the main kernel, not the FE; the main kernel returns the result to the FE. Messages and Print have always appeared as "Print" cells tagged with the subkernel ID. Per the docs, $FrontEnd is either a FrontEndObject or Null, and the two options discussed for interfaces are "notebook" and "text-based." I do not see a statement in the Documentation that is directly contradicted, even if the docs are not complete and lucid on every point (as we well know they are not on many points). $\endgroup$
    – Michael E2
    Dec 22, 2021 at 16:36
  • $\begingroup$ @MichaelE2 Do you think that printing BoxData[1] is the expected behavior? My point is that according to the cited Documentation statement CellPrint must work exactly as Print in a subkernel, but it doesn't. $\endgroup$ Dec 22, 2021 at 18:01
  • 1
    $\begingroup$ The docs say that Print is applied to the cell contents. The cell contents of the cell produced by CellPrint[1] are BoxData["1"] (look at Cell > Show Expression in a regular kernel/FE). $\endgroup$
    – Michael E2
    Dec 22, 2021 at 18:07
  • $\begingroup$ @MichaelE2 Thank you for the explanation. I removed the bugs tag and included your comment in my answer. $\endgroup$ Dec 22, 2021 at 18:18

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.