Waiting up to a full minute for rteval to stop on ctrl-c can be frustrating. Likewise, if a very short run is requested (e.g. for testing rteval itself) rounding it up to a minute is not polite. Signed-off-by: Crystal Wood <crwood@xxxxxxxxxx> --- rteval/__init__.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/rteval/__init__.py b/rteval/__init__.py index 5d43615af5bb..72e3412a860e 100644 --- a/rteval/__init__.py +++ b/rteval/__init__.py @@ -32,12 +32,11 @@ RTEVAL_VERSION = version.RTEVAL_VERSION earlystop = False -stopsig_received = False +stopsig = threading.Event() def sig_handler(signum, frame): """ Handle SIGINT (CTRL + C) or SIGTERM (Termination signal) """ if signum in (signal.SIGINT, signal.SIGTERM): - global stopsig_received - stopsig_received = True + stopsig.set() print("*** stop signal received - stopping rteval run ***") else: raise RuntimeError(f"SIGNAL received! ({signum})") @@ -208,8 +207,8 @@ class RtEval(rtevalReport): currtime = time.time() rpttime = currtime + report_interval load_avg_checked = 5 - while (currtime <= stoptime) and not stopsig_received: - time.sleep(60.0) + while (currtime <= stoptime) and not stopsig.is_set(): + stopsig.wait(min(stoptime - currtime, 60.0)) if not measure_profile.isAlive(): stoptime = currtime earlystop = True @@ -238,7 +237,7 @@ class RtEval(rtevalReport): signal.signal(signal.SIGTERM, signal.SIG_DFL) except RuntimeError as err: - if not stopsig_received: + if not stopsig.is_set(): raise RuntimeError(f"appeared during measurement: {err}") finally: -- 2.43.0