vmprof-python
vmprof-python copied to clipboard
Persist information about whether and how vmprof is running
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.
vmprof.is_enabled() and vmprof.get_profile_path() are released in version 0.4.4