diff --git a/common.py b/common.py index 44d9be4..6f1fcd4 100644 --- a/common.py +++ b/common.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 # -# (c) 2019-2023 Antmicro +# (c) 2019-2026 Antmicro # License: Apache-2.0 # @@ -11,6 +11,7 @@ import subprocess import sys import re import platform +import time # Increase major number for general changes, middle number for smaller changes @@ -45,6 +46,14 @@ def pid_running(pid): def file_exists(filename: str): return os.path.exists(filename) +# Spin and wait until function succeeds +def spinloop(f, wait: float, tries: int): + for _ in range(tries): + if f(): + return True + time.sleep(wait) + return False + # Convert a string to float, also when the separator is a comma def stof(s): diff --git a/graph.py b/graph.py index 34d5734..e7d3ff0 100644 --- a/graph.py +++ b/graph.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 # -# (c) 2019-2023 Antmicro +# (c) 2019-2026 Antmicro # License: Apache-2.0 # diff --git a/sargraph.py b/sargraph.py index 1c79d21..ceafd16 100755 --- a/sargraph.py +++ b/sargraph.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 # -# (c) 2019-2023 Antmicro +# (c) 2019-2026 Antmicro # License: Apache-2.0 # @@ -88,13 +88,9 @@ if args.command[0] == "start": ) # Spinloop to see whether the subprocess even starts - attempts = 5 - while attempts: - time.sleep(0.1) - if file_exists(socket_path): - print(f"Session '{args.session}' started") - sys.exit(0) - attempts -= 1 + if spinloop(lambda: file_exists(socket_path), 0.1, 5): + print(f"Session '{args.session}' started") + sys.exit(0) fail("Session did not start") @@ -108,14 +104,10 @@ elif args.command[0] == "stop": send(args.session, f"command:q:{args.command[1]}") # Spinloop to see whether the subprocess even dies - attempts = 5 - while attempts: - time.sleep(0.5) - if not file_exists(socket_path): - print(f"Session '{args.session}' killed") - sys.exit(0) - attempts -= 1 - + if spinloop(lambda: not file_exists(socket_path), 0.5, 5): + print(f"Session '{args.session}' killed") + sys.exit(0) + fail("Session did not respond") diff --git a/watch.py b/watch.py index e1a9726..008a870 100644 --- a/watch.py +++ b/watch.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 # -# (c) 2019-2023 Antmicro +# (c) 2019-2026 Antmicro # License: Apache-2.0 # @@ -131,7 +131,7 @@ class Watcher(abc.ABC): self.socket, self.socket_path = get_socket(self.session) - # Was a graph alreay produced by save command from sargraph? + # Was a graph already produced by save command from sargraph? self.dont_plot = False # Should we die?