Skip to content

Persist information about whether and how vmprof is running #123

@Julian

Description

@Julian

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions