12

I have long-running python programs that do not have sufficient internal logging. I'd like to be able to attach to a running python program and find out it's current line number. Ideally, I'd like to do this without interrupting its execution, but I'm happy to have it pause, print the stack t race, and then keep going.

I could easily type control-C, but that would kill the program.

Is there any way to do this?

2
  • If you're fine with debugging kind of approach, try out pdb - docs.python.org/3/library/pdb.html Commented Jan 3, 2019 at 15:29
  • 1
    Thanks. pdb works great if you launch the program under pdb, but it doesn't look like it can attach to a running python program or perform introspection. Commented Jan 3, 2019 at 15:40

1 Answer 1

10

Seems like py-spy is what you are looking for.

It can connect to running process without modifying and show what it currently does by using the command:

py-spy dump --pid 12345
Sign up to request clarification or add additional context in comments.

2 Comments

py-spy it is! Thanks!
Note that it does 'modify' the running process in that it pauses it to read what it's doing in a consistent state. To avoid that, for example if your process seems to be hanging somewhere anyway (in my case it was hanging on a network read somewhere in a standard library, so the stack wasn't going to change), you can use py-spy dump --nonblocking --pid 12345

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.