vmprof-python icon indicating copy to clipboard operation
vmprof-python copied to clipboard

Persist information about whether and how vmprof is running

Open Julian opened this issue 8 years ago • 1 comments

It'd be nice to have an API for querying whether vmprof is running, and further how it's configured, particularly, how it's outputting.

Some sample code for this use case:

@attr.s
class _VMProf(object):

    path = attr.ib(default=None)

    def disable(self):
        import vmprof
        path, self.path = self.path, None
        vmprof.disable()
        sys.stdout.write("Finished profiling. Logged to {!r}\n".format(path))

    def enable(self):
        import vmprof

        fileno, self.path = tempfile.mkstemp(prefix="vmprof", suffix=".output")

        try:
            vmprof.enable(fileno, memory=True)
        except Exception:
            traceback.print_exc(file=sys.stderr)
            self.path = None
            os.remove(self.path)
            return

        sys.stdout.write(
            "Enabling vmprof... Outputting to {!r}.\n".format(self.path),
        )

    def handle_signal(self, signum, frame):
        if self.path is not None:
            self.disable()
        else:
            self.enable()


signal.signal(signal.SIGUSR2, _VMProf().handle_signal)

Having that entire signal handler API be exposed (e.g. vmprof.attach_to_signal(...)) might also be nice, although possibly outside the scope of this ticket.

Julian avatar Mar 16 '17 18:03 Julian

vmprof.is_enabled() and vmprof.get_profile_path() are released in version 0.4.4

planrich avatar Apr 24 '17 14:04 planrich