From 2271a02a0328b28306712c5fd87cc21d17390058 Mon Sep 17 00:00:00 2001 From: Yonatan Goldschmidt Date: Sun, 20 Jun 2021 03:46:12 +0300 Subject: [PATCH] utils: Extract wait_for_file_by_prefix() --- gprofiler/python.py | 16 +++------------- gprofiler/utils.py | 11 +++++++++++ 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/gprofiler/python.py b/gprofiler/python.py index 70550bfb4..41d425a55 100644 --- a/gprofiler/python.py +++ b/gprofiler/python.py @@ -26,6 +26,7 @@ run_process, start_process, wait_event, + wait_for_file_by_prefix, ) logger = logging.getLogger(__name__) @@ -217,24 +218,13 @@ def start(self): else: self.process = process - def _glob_output(self) -> List[str]: - # important to not grab the transient data file - return glob.glob(f"{str(self.output_path)}.*") - - def _wait_for_output_file(self, timeout: float) -> Path: - wait_event(timeout, self._stop_event, lambda: len(self._glob_output()) > 0) - - output_files = self._glob_output() - # All the snapshot samples should be in one file - assert len(output_files) == 1 - return Path(output_files[0]) - def _dump(self) -> Path: assert self.process is not None, "profiling not started!" self.process.send_signal(self.dump_signal) try: - output = self._wait_for_output_file(self.dump_timeout) + # important to not grab the transient data file - hence the following '.' + output = wait_for_file_by_prefix(f"{self.output_path}.", self.dump_timeout, self._stop_event) # PyPerf outputs sampling & error counters every interval (after writing the output file), print them. # also, makes sure its output pipe doesn't fill up. # using read1() which performs just a single read() call and doesn't read until EOF diff --git a/gprofiler/utils.py b/gprofiler/utils.py index c03c249ef..6bfaeeafa 100644 --- a/gprofiler/utils.py +++ b/gprofiler/utils.py @@ -6,6 +6,7 @@ import datetime import errno import fcntl +import glob import logging import os import platform @@ -127,6 +128,16 @@ def poll_process(process, timeout: float, stop_event: Event): raise +def wait_for_file_by_prefix(prefix: str, timeout: float, stop_event: Event) -> Path: + glob_pattern = f"{prefix}*" + wait_event(timeout, stop_event, lambda: len(glob.glob(glob_pattern)) > 0) + + output_files = glob.glob(glob_pattern) + # All the snapshot samples should be in one file + assert len(output_files) == 1 + return Path(output_files[0]) + + def run_process( cmd: Union[str, List[str]], stop_event: Event = None,