Hallo,
is there a way to keep a python kernel open to transfer some global variables/objects from one Python-node to the next? This would be useful if you have an unpickable object like a (database or network)-connection
Hey there,
I think it is not possible to “keep the kernel” open.
If you want to pass variables from one python node to another you can try to use:
- Flow variables in case you have individual variables or lists etc.
knime.scripting.io.flow_variables: Dict[str, Any]= {} A dictionary of flow variables provided by the KNIME workflow. New flow variables can be added to the output of the node by adding them to the dictionary. Supported flow variable types are numbers, strings, booleans and lists thereof.
- You should be able to add a new variable to this dictionary and then access it in a different node (be it Python Script node or any other)
- Full documentation you can find here: Python Script API — KNIME Python API documentation
With regards to unpickable objects I do not think that is possible with Python Script nodes.
This is, however, possible if you create a KNIME Python Extension using ConnectionPortObjects:
classknime.extension.ConnectionPortObject(spec: PortObjectSpec)
Connection port objects are a special type of port objects which support dealing with non-serializable objects such as database connections or web sessions.
Connection port objects are passed downstream by ensuring that the same Python process is used to execute subsequent nodes. ConnectionPortObjects must provide the data in the to_connection_data and create new instances from the same data in from_connection_data. A reference to the data Python object is maintained and handed to downstream nodes. So the data does not need to be serializable/picklable.
Full documentation for this you can find here: Python Extension Development (Labs) — KNIME Python API documentation
2 Likes