benfred / py-spy

Sampling profiler for Python programs
MIT License
12.87k stars 431 forks source link

Programmatically start/stop py-spy recording #652

Open michaeleisel opened 9 months ago

michaeleisel commented 9 months ago

I want to run a py-spy profiling, e.g. py-spy record -o /tmp/py_flamegraph.svg --idle python3 ${file}, but only profile a certain piece of the code, because the code before and after it adds noise in the flamegraph. I could use attach to a running pid, but it's cumbersome and tricky to ensure that I attach and detach at the appropriate times. Instead, I'd like something simple like:

py_spy.begin_recording()
# code
py_spy.end_recording()

and maybe a flag for the CLI telling it not to automatically begin, but rather to let the profiling begin programmatically

flying-sheep commented 6 months ago

Yeah, that would be great! But please use the search function: https://github.com/benfred/py-spy/issues/531