What is the proper way to link a process in Livebook? (e.g. when running Flows/GenStage in separate processes) #1614
-
I’m playing with flow, which starts a bunch of processes in the background. All is good when the flow isn’t interrupted by any errors. But when an error happens, Livebook seems to be confused. The error is logged in the console, and a flash message appears in the upper right corner (“Evaluation process terminated - shutdown”). But then the underlying runtime seems to die (I'm running an Elixir standalone runtime). When I try to rerun the code-cell, any variables defined upstream are not defined anymore. I think I have to do something with Kino.start_child/1 and apply supervisable flows somehow, but I’m not sure how to stitch those together. I surely can make a module containing the flow logic, but I still don’t know how to create a child_spec for it. I’d like to collect the results back in the calling code-cell/process, to get the final result and print it in the cell. Any ideas for how to tackle this? It’s probably more generic in the sense that any process started this way won’t be linked properly. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
Hey @linusdm! Whenever we do When you do define your own module like defmodule MyFlow do
use Flow
def start_link(_) do
# ...
|> Flow.start_link()
end
end
Kino.start_child(MyFlow) If you don't want to define a module, you can write down the child spec explicitly: flow = ...
Kino.start_child(%{id: MyFlow, start: {Flow, :start_link, [flow]}}) In either case it runs in a separate process, so to get results back in the cell we would need to send them explicitly. @josevalim what's the best way to do that? |
Beta Was this translation helpful? Give feedback.
-
Although this setup is perfect, and allows re-evaluation of code-cells, without having dangling processes, it would be nice to have UI controls to start and stop the underlying processes/flow. Then you could start the process on demand, and also stop it with a button or something. That's more flexible than only being able to re-evalute the code cell. Also, the state could be reflected in some kind of green/red dot (similar to how the execution of the cells is being visualized). Just thinking out loud... Probably doable in a smart cell? |
Beta Was this translation helpful? Give feedback.
Hey @linusdm!
Whenever we do
start_link
explicitly, it ties the given process to Livebook evaluation process, so if the linked process crashes, so does the evaluation process, and consequently all the state/variables is gone. Usually we would put such process in our app suprevision tree and in Livebook the counterpart is to useKino.start_child/1
, which makes sure the process is terminated when you reevaluate the cell.When you do define your own module like
MyFlow
and douse Flow
, it defineschild_spec
for you, so you can do this:If you don't want to define a mod…