From c6a68bd9f09eca19dcddf704434716719793e921 Mon Sep 17 00:00:00 2001 From: Oussama Teffahi Date: Wed, 21 Feb 2024 17:12:44 +0100 Subject: [PATCH 1/2] Remove reading from stdin --- examples/z_pub.py | 1 + examples/z_pub_thr.py | 1 + examples/z_pull.py | 15 ++++++--------- examples/z_queryable.py | 11 +++-------- examples/z_storage.py | 9 +++------ examples/z_sub.py | 9 +++------ examples/z_sub_queued.py | 9 +++------ examples/z_sub_thr.py | 9 +++------ 8 files changed, 23 insertions(+), 41 deletions(-) diff --git a/examples/z_pub.py b/examples/z_pub.py index f9ebbb53..f9ad6635 100644 --- a/examples/z_pub.py +++ b/examples/z_pub.py @@ -74,6 +74,7 @@ def main(): print(f"Declaring Publisher on '{key}'...") pub = session.declare_publisher(key) + print("Press CTRL-C to quit...") for idx in itertools.count() if args.iter is None else range(args.iter): time.sleep(1) buf = f"[{idx:4d}] {value}" diff --git a/examples/z_pub_thr.py b/examples/z_pub_thr.py index 7d9896c3..dc67e725 100644 --- a/examples/z_pub_thr.py +++ b/examples/z_pub_thr.py @@ -69,6 +69,7 @@ def main(): session = zenoh.open(conf) pub = session.declare_publisher('test/thr', congestion_control=congestion_control) + print("Press CTRL-C to quit...") while True: pub.put(data) diff --git a/examples/z_pull.py b/examples/z_pull.py index e2e2e77f..6ce80657 100644 --- a/examples/z_pull.py +++ b/examples/z_pull.py @@ -12,6 +12,7 @@ # ZettaScale Zenoh Team, # +import itertools import sys import time from datetime import datetime @@ -73,17 +74,13 @@ def main(): session = zenoh.open(conf) print("Declaring Subscriber on '{}'...".format(key)) - sub = session.declare_pull_subscriber(key, listen, reliability=Reliability.RELIABLE()) - print("Press to pull data...") - c = '\0' - while c != 'q': - c = sys.stdin.read(1) - if c == '': - time.sleep(1) - else: - sub.pull() + print("Press CTRL-C to quit...") + for idx in itertools.count(): + time.sleep(1) + print(f"[{idx:4d}] Pulling...") + sub.pull() sub.undeclare() session.close() diff --git a/examples/z_queryable.py b/examples/z_queryable.py index 17bf7a14..4223afba 100644 --- a/examples/z_queryable.py +++ b/examples/z_queryable.py @@ -84,14 +84,9 @@ def main(): print("Declaring Queryable on '{}'...".format(key)) queryable = session.declare_queryable(key, queryable_callback, complete) - print("Enter 'q' to quit...") - c = '\0' - while c != 'q': - c = sys.stdin.read(1) - if c != 'q': - print("getting") - session.get(key, print, consolidation=zenoh.QueryConsolidation.NONE()) - time.sleep(1) + print("Press CTRL-C to quit...") + while True: + time.sleep(1) queryable.undeclare() session.close() diff --git a/examples/z_storage.py b/examples/z_storage.py index 0589aad5..b993aa14 100644 --- a/examples/z_storage.py +++ b/examples/z_storage.py @@ -98,12 +98,9 @@ def main(): print("Declaring Queryable on '{}'...".format(key)) queryable = session.declare_queryable(key, query_handler, complete) - print("Enter 'q' to quit...") - c = '\0' - while c != 'q': - c = sys.stdin.read(1) - if c == '': - time.sleep(1) + print("Press CTRL-C to quit...") + while True: + time.sleep(1) sub.undeclare() queryable.undeclare() diff --git a/examples/z_sub.py b/examples/z_sub.py index fcd63e58..65ba6d33 100644 --- a/examples/z_sub.py +++ b/examples/z_sub.py @@ -78,12 +78,9 @@ def listener(sample: Sample): # will be immediately undeclared. sub = session.declare_subscriber(key, listener, reliability=Reliability.RELIABLE()) - print("Enter 'q' to quit...") - c = '\0' - while c != 'q': - c = sys.stdin.read(1) - if c == '': - time.sleep(1) + print("Press CTRL-C to quit...") + while True: + time.sleep(1) # Cleanup: note that even if you forget it, cleanup will happen automatically when # the reference counter reaches 0 diff --git a/examples/z_sub_queued.py b/examples/z_sub_queued.py index dfd9850d..46157497 100644 --- a/examples/z_sub_queued.py +++ b/examples/z_sub_queued.py @@ -84,12 +84,9 @@ def consumer(): t = Thread(target=consumer) t.start() - print("Enter 'q' to quit...") - c = '\0' - while c != 'q': - c = sys.stdin.read(1) - if c == '': - time.sleep(1) + print("Press CTRL-C to quit...") + while True: + time.sleep(1) # Cleanup: note that even if you forget it, cleanup will happen automatically when # the reference counter reaches 0 diff --git a/examples/z_sub_thr.py b/examples/z_sub_thr.py index f9b45d05..6d9ae57a 100644 --- a/examples/z_sub_thr.py +++ b/examples/z_sub_thr.py @@ -96,12 +96,9 @@ def main(): # Only do this if your callback runs faster than the minimum expected delay between two samples. sub = session.declare_subscriber("test/thr", zenoh.Closure((listener, report)), reliability=Reliability.RELIABLE()) - print("Enter 'q' to quit...") - c = '\0' - while c != 'q': - c = sys.stdin.read(1) - if c == '': - time.sleep(1) + print("Press CTRL-C to quit...") + while True: + time.sleep(1) sub.undeclare() session.close() From 8c84fb9ef40150cf57058db04aa7a50efa208a7c Mon Sep 17 00:00:00 2001 From: Oussama Teffahi Date: Thu, 22 Feb 2024 12:34:15 +0100 Subject: [PATCH 2/2] Update examples tests --- tests/examples_check.py | 64 +++++++++++++++-------------------------- 1 file changed, 23 insertions(+), 41 deletions(-) diff --git a/tests/examples_check.py b/tests/examples_check.py index 87c7b9c8..6c2d3e93 100644 --- a/tests/examples_check.py +++ b/tests/examples_check.py @@ -10,11 +10,12 @@ # Contributors: # ZettaScale Zenoh team, # -from os import path +from os import path, getpgid, killpg +from signal import SIGINT from subprocess import Popen, PIPE import time -examples = path.dirname(path.realpath(__file__)).replace('tests', 'examples') +examples = path.realpath(__file__).split("/tests")[0]+"/examples/" tab = "\t" ret = "\r\n" KILL = -9 @@ -25,7 +26,7 @@ def __init__(self, p, args=None) -> None: args = [] self.name = p print(f"starting {self.name}") - self.process: Popen = Popen(["python3", path.join(examples, p), *args], stdin=PIPE, stdout=PIPE, stderr=PIPE) + self.process: Popen = Popen(["python3", path.join(examples, p), *args], stdin=PIPE, stdout=PIPE, stderr=PIPE, start_new_session=True) self.start = time.time() self.end = None self._stdouts = [] @@ -51,6 +52,10 @@ def wait(self): if self.end is None: self.end = time.time() return code + def interrupt(self): + # send SIGINT to process group + pgid = getpgid(self.process.pid) + killpg(pgid, SIGINT) @property def stdout(self): self._stdouts.extend(line.decode('utf8') for line in self.process.stdout.readlines()) @@ -85,22 +90,14 @@ def time(self): time.sleep(1) pub = Pyrun("z_pub.py", ["--iter=2"]) time.sleep(4) -try: - pull.process.stdin.write(b"\n") - pull.process.stdin.flush() - time.sleep(1) - pull.process.stdin.write(b"q\n") - pull.process.stdin.flush() - pull.process.stdin.close() -except Exception as e: - pull.dbg() - errors.append(f"pull stdin sequence failed: {e}") + +pull.interrupt() if pub.status(): pub.dbg() errors.append(pub.status()) -if pull.status(): +if pull.status(KILL): pull.dbg() - errors.append(pull.status()) + errors.append(pull.status(KILL)) subout = "".join(pull.stdout) if not ("Received PUT ('demo/example/zenoh-python-put': 'Put from Python!')" in subout): errors.append("z_pull didn't catch put") @@ -120,15 +117,10 @@ def time(self): queryable.dbg() errors.append("z_get didn't get a response from z_queryable") -try: - queryable.process.stdin.write(b"q\n") - queryable.process.stdin.flush() - queryable.process.stdin.close() -except Exception as e: - errors.append(f"queryable stdin sequence failed: {e}") -if queryable.status(): +queryable.interrupt() +if queryable.status(KILL): queryable.dbg() - errors.append(queryable.status()) + errors.append(queryable.status(KILL)) queryableout = "".join(queryable.stdout) if not ("Received Query 'demo/example/zenoh-python-queryable'" in queryableout): errors.append("z_queryable didn't catch query") @@ -163,15 +155,10 @@ def time(self): if any(("z_get" in error) for error in errors): get.dbg() -try: - sub.process.stdin.write(b"q\n") - sub.process.stdin.flush() - sub.process.stdin.close() -except Exception as e: - errors.append(f"pub stdin sequence failed: {e}") -if sub.status(): +sub.interrupt() +if sub.status(KILL): sub.dbg() - errors.append(sub.status()) + errors.append(sub.status(KILL)) subout = "".join(sub.stdout) if not ("Received PUT ('demo/example/zenoh-python-put': 'Put from Python!')" in subout): errors.append("z_sub didn't catch put") @@ -182,15 +169,10 @@ def time(self): if any(("z_sub" in error) for error in errors): sub.dbg() -try: - storage.process.stdin.write(b"q\n") - storage.process.stdin.flush() - storage.process.stdin.close() -except Exception as e: - errors.append(f"storage stdin sequence failed: {e}") -if storage.status(): +storage.interrupt() +if storage.status(KILL): storage.dbg() - errors.append(storage.status()) + errors.append(storage.status(KILL)) storageout = "".join(storage.stdout) if not ("Received PUT ('demo/example/zenoh-python-put': 'Put from Python!')" in storageout): errors.append("z_storage didn't catch put") @@ -210,10 +192,10 @@ def time(self): pub_thr.process.kill() if sub_thr.status(KILL): sub_thr.dbg() - errors.append(sub_thr.status()) + errors.append(sub_thr.status(KILL)) if pub_thr.status(KILL): pub_thr.dbg() - errors.append(pub_thr.status()) + errors.append(pub_thr.status(KILL)) if len(errors):