When an important process needs to be kept alive, there are monitoring tools to restart them if they die (e.g. god tasks in ruby), in my case I have an overnight scraping task that I need to have done by the morning. My code maintains state so all that is required is a watchful eye and a few shift-enters, but not if I'm asleep!
My question: Is there any way to detect when a Kernel
dies and automatically restart that Kernel
and run specific code or perhaps enqueue specific cells to evaluate?
More Details:
My cell is running happily in the notebook, and then at random intervals for unknown reasons the notebook's kernel silently dies (you can tell because all the symbol colorings change), the notebook itself is fine, but the symbol table is empty. However my code maintains state, so to resume all I have to do to resume processing where I left off is to shift enter a single cell.
Answer
Assuming FrontEnd
survives, prepare 3 cells:
(*init cell, won't be needed later*)
state = CurrentValue[EvaluationNotebook[], {"TaggingRules", "state"}] = 0;
SetOptions[ #,
{CellTags -> {"Procedure"}, ShowCellTags -> True}
]& /@ {NextCell[], NextCell @ NextCell[]};
CurrentValue[$FrontEndSession, "ClearEvaluationQueueOnKernelQuit"] = False;
(*main procedure cell*)
Print["cell init session id: ", $SessionID];
Do[
CurrentValue[EvaluationNotebook[], {"TaggingRules", "state"}] = i;
Print[i];
If[ MemberQ[{2, 3, 4}, i], Quit[] ]
,
{i, state + 1, 5}
]
(* restarting procedure *)
If[
# < 5
,
state = #;
Print["procedure was interrupted at state: ", #];
NotebookLocate["Procedure"];
SelectionEvaluate @ EvaluationNotebook[];
] & @ CurrentValue[EvaluationNotebook[], {"TaggingRules", "state"}]
Select them all and evaluate. TaggingRules
are not important, it's just minimal example of preserving state.
cell init session id: 25310486074412977156
1
2
procedure was interrupted at state: 2
cell init session id: 25310486139919804003
3
procedure was interrupted at state: 3
cell init session id: 25310486231460654483
4
procedure was interrupted at state: 4
cell init session id: 25310486323762607669
5
Comments
Post a Comment