From 4f2d9fa66df830e06f5ad9c7b73831ad67cac64a Mon Sep 17 00:00:00 2001 From: douglas-raillard-arm Date: Wed, 24 Mar 2021 10:19:23 +0000 Subject: [PATCH] target: Add Target.background(timeout=...) parameter Use a Timer daemonic thread to cancel the command after the given timeout. --- devlib/target.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/devlib/target.py b/devlib/target.py index 8f37b0a..8a577b1 100644 --- a/devlib/target.py +++ b/devlib/target.py @@ -559,9 +559,14 @@ class Target(object): strip_colors=strip_colors, will_succeed=will_succeed) def background(self, command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, as_root=False, - force_locale='C'): + force_locale='C', timeout=None): command = self._prepare_cmd(command, force_locale) - return self.conn.background(command, stdout, stderr, as_root) + bg_cmd = self.conn.background(command, stdout, stderr, as_root) + if timeout is not None: + timer = threading.Timer(timeout, function=bg_cmd.cancel) + timer.daemon = True + timer.start() + return bg_cmd def invoke(self, binary, args=None, in_directory=None, on_cpus=None, redirect_stderr=False, as_root=False, timeout=30):