Skip to content

Commit

Permalink
utils: Extract wait_for_file_by_prefix()
Browse files Browse the repository at this point in the history
  • Loading branch information
Jongy committed Jun 20, 2021
1 parent cd78f35 commit 2271a02
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
16 changes: 3 additions & 13 deletions gprofiler/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
run_process,
start_process,
wait_event,
wait_for_file_by_prefix,
)

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions gprofiler/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import datetime
import errno
import fcntl
import glob
import logging
import os
import platform
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 2271a02

Please sign in to comment.