benfred / py-spy

Sampling profiler for Python programs
MIT License
12.16k stars 401 forks source link

Terminal broken and no output file after running py-spy in background and stopping it with SIGINT #557

Open AkaZecik opened 1 year ago

AkaZecik commented 1 year ago

Hi, I have a server running locally and I want to profile it by making multiple identical HTTP requests from terminal. I would like to write a script that starts py-spy record ... in the background (passing PID of the local server), executes a command that makes the requests, and then stops py-spy.

Here is my attempt:

sudo env "PATH=$PATH" py-spy record --subprocesses --format speedscope --output $2 --pid $1 &
PY_SPY_PID=$!  # store pid of py-spy's process
./make_requests.sh
kill --signal SIGINT $PY_SPY_PID

However, after this script finishes, my terminal is broken, i.e. no input is printed back to the screen, and outputs of commands are badly aligned. Also, no output file from py-spy is created. The same thing happens if I don't call ./make_requests.sh.

Is there a way to work around this issue? Is this a bug or just an unusual use-case?

Either way, I propose for py-spy to add support for running and stopping in background, e.g. by adding a flag --run-in-background or simply supporting the above scenario.

My setup: Ubuntu 22.04 LTS, 5.19.0-32-generic, x86_64