Neither Python example was registered as a test, so `ctest -L examples` in CI skipped them entirely -- which is how 08's missing Python node went unnoticed. Add a kpn_python_example() helper (gated on KPN_BUILD_PYTHON) that runs each script with PYTHONPATH pointed at the freshly-built module, so it does not depend on cwd or a hard-coded build/python path, and register 07 and 08. Also del the network in 07 for deterministic teardown.
38 lines
952 B
Python
38 lines
952 B
Python
"""
|
|
07_python_network — hello pipeline with a Python node in the middle.
|
|
|
|
Graph:
|
|
[ProduceNode] --int--> [py_double] --int--> [PrintItNode]
|
|
|
|
ProduceNode and PrintItNode are C++ nodes wrapped in VariantNodeWrapper.
|
|
py_double is a pure Python callable — doubles its input using Python arithmetic.
|
|
"""
|
|
|
|
import sys
|
|
import time
|
|
sys.path.insert(0, "build/python")
|
|
|
|
import kpn_python as kpn
|
|
|
|
def py_double(x: int) -> int:
|
|
return x * 2
|
|
|
|
net = kpn.Network()
|
|
|
|
net.add("src", kpn.make_produce())
|
|
net.add_node("dbl", py_double, inputs=["int"], outputs=["int"])
|
|
net.add("sink", kpn.make_print_it())
|
|
|
|
net.connect("src", 0, "dbl", 0)
|
|
net.connect("dbl", 0, "sink", 0)
|
|
|
|
net.build()
|
|
net.start()
|
|
time.sleep(0.1)
|
|
net.stop()
|
|
|
|
# Drop the network deterministically: it holds the Python callable, which forms
|
|
# a reference cycle via globals(). Deleting the global breaks it so the network
|
|
# is reclaimed now rather than lingering to interpreter shutdown.
|
|
del net
|