If one of our linux system is overloaded, then I would like to take a snapshot of what is going on.

99% of our code is Python, that's why I would like to get a stacktrace of all running python interpreters.

I have no clue how to get the stacktrace of python processes.

This question is only about how to get the stacktrace. The condition which checks if I want it to happen is a different question (which is already solved).

I know that it is possible to get the stacktrace by using a signal handler (See here), but I would like to avoid this. The python source code should not get modified to solve this.

  • Just curious - how would stack traces help you determine the cause of system overload? – Mawg says reinstate Monica Jul 30 '18 at 9:42
  • 1
    @Mawg you want to know why stacktraces help me? Because I can see what is going on. If I just know "some python processes are running at 100% CPU" I do not know much. But if I can see the stacktraces, then I can see what the python processes are actually doing. – guettli Jul 30 '18 at 12:38
  • Thanks (+1) I was wondering how, without looking in detail at lots of stack dumps, you would know which one was causing the 100% CPU. Can't you give them all unique process name, then run top or look at the Windows taskmgr, to see which process is hogging CPU? And can't you limit their CPU somehow? In short, I am wondering of this is really the best question for you to ask :-) – Mawg says reinstate Monica Jul 30 '18 at 13:09
  • @Mawg if this is not the best question, which one would be better? – guettli Jul 30 '18 at 13:11
  • To me, it sounds like a "which process is eating my CPU" question? Wouldn't top help? Can't you give them all unique process names, have another process which is a watchdog and when CPU > 90% it snapshots the offender? Or, can't you use the O/S's parameters to create process/thread, to limit CPU usage, as you can stack size? Maybe adjust priority? You might get better help on unix.stackexchange.com – Mawg says reinstate Monica Jul 30 '18 at 13:39

The tool pyrasite:

Tools for injecting arbitrary code into running Python processes.

This way you can get a stacktrace of running python processes.

No need to modify the python source and no need to work with signal handlers :-)


The tool python-hunter could be used to extract the stacktraces

Hunter is a flexible code tracing toolkit, not for measuring coverage, but for debugging, logging, inspection and other nefarious purposes. It has a Python API, terminal activation (see Environment variable activation). and supports tracing other processes


The too pyflame can be used to dump stacktraces of running interpreters:

Is There A Way To Just Dump Stack Traces?

Yes, use the -d option.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.