Skip to content

Commit 4ad738c

Browse files
committed
Add some docs to Python ex06.
1 parent 84ae12d commit 4ad738c

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

python_examples/ex06_async_nodes.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,20 @@
3333

3434
@ports(inputs=["start", "goal"], outputs=["command"])
3535
class MyAsyncNode(AsyncActionNode):
36+
def __init__(self, name, config):
37+
super().__init__(name, config)
38+
self.halted = False
39+
3640
def run(self):
3741
start = np.asarray(self.get_input("start"))
3842
goal = np.asarray(self.get_input("goal"))
3943

44+
# Here we write an imperative-looking loop, but we place a `yield` call
45+
# at each iteration. This causes the coroutine to yield back to the
46+
# caller until the next iteration of the tree, rather than block the
47+
# main thread.
4048
t0 = time.time()
41-
while (t := time.time() - t0) < 1.0:
49+
while (t := time.time() - t0) < 1.0 and not self.halted:
4250
command = (1.0 - t) * start + t * goal
4351
self.set_output("command", command)
4452
yield
@@ -47,7 +55,8 @@ def run(self):
4755
return NodeStatus.SUCCESS
4856

4957
def on_halted(self):
50-
print("Aborted")
58+
# This will be picked up in the main iteration above.
59+
self.halted = True
5160

5261

5362
@ports(inputs=["value"])

0 commit comments

Comments
 (0)