# Monitoring Parallel Processes

I have a problem where my For loop will not get rid of my PrintTemporary monitor, and it means I end up with loads and loads of progress bars.

(* set number of kerlnels to use *)
numkernel = 4;
(* how high do you want to count to? *)
iterations = 15;

(* for monitoring progress over multiple kernels *)
progress[current_, total_] :=

Column[{StringRiffle[ToString /@ {current, total}, " of "],
ProgressIndicator[current, {0, total}]}, Alignment -> Center]

(* counting very slowly *)
takeyourtime[howmany_, kernelnum_] := Module[{},
For[i = 1, i <= howmany, i++,
status[[Key[\$KernelID]]] = <|
"Kernel" -> "Kernel " <> ToString@kernelnum,
"Monitor" -> progress[i, howmany]|>;
Pause;
];
]

(* will count up to iterations, k times over numkernel kernels *)
For[k = 1, k <= 3, k = k + 1;,

Print[k];

(*clean up*)
Clear[parrallel];

(* how to divide the counting *)
division = Round[iterations/numkernel];

(* remainder on the first kernel *)
divisionmod =
Round[iterations/numkernel] + Mod[iterations, numkernel];

(* put the first evaluation in, with the remainder*)
parrallel = {Hold[ParallelSubmit[{takeyourtime[divisionmod, 1]}]]};

(*for number of kernels, set up the parrallel stuff *)
For[i = 2, i <=  numkernel, i++ ,
parrallel =
Append[parrallel,
Hold[ParallelSubmit[{takeyourtime[division, 1]}]]];
(*to this, so we can add i and do the monitoring better,
have to be careful when expressions are evaluated,
want to keep that for the parralel bit *)
parrallel[[i]] =
StringReplace[ToString[parrallel[[i]]],
", i]" -> ", " <> ToString[i] <> "]"];
parrallel[[i]] = ToExpression[parrallel[[i]]];
] ;

LaunchKernels[];

(* the monitoring variable we will print *)
status =
Association @@
ParallelTable[$$KernelID -> <|"Kernel" -> "", "Monitor" -> ""|>, {i,$$KernelCount}];

(*distribute the required definitions around the kernels *)
DistributeDefinitions[takeyourtime, division, divisionmod ];

SetSharedVariable[status];

(* this is the monitoring bit *)
PrintTemporary[
Dynamic[Row[
Riffle[Column[#, Alignment -> Center] & /@
Query[Values, Values]@Select[#"Monitor" =!= "" &]@status,
Spacer]]]];

(*execute the parralel process *)
results = WaitAll[ReleaseHold[parrallel]];

CloseKernels[];
] • Related (118663). Feb 12, 2020 at 23:12

NotebookDelete[NextCell[]];

In the end of the For loop.