Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Statistical Profiling in Python

I would like to know that the Python interpreter is doing in my production environments.

Some time ago I wrote a simple tool called live-trace which runs a daemon thread which collects stacktraces every N milliseconds.

But signal handling in the interpreter itself has one disadvantage:

Although Python signal handlers are called asynchronously as far as the Python user is concerned, they can only occur between the “atomic” instructions of the Python interpreter. This means that signals arriving during long calculations implemented purely in C (such as regular expression matches on large bodies of text) may be delayed for an arbitrary amount of time.

Source: https://docs.python.org/2/library/signal.html

How could I work around above constraint and get a stacktrace, even if the interpreter is in some C code for several seconds?

Related: https://github.com/23andMe/djdt-flamegraph/issues/5

like image 283
guettli Avatar asked Feb 28 '18 13:02

guettli


People also ask

What is profiling in python?

Introduction to the profilers cProfile and profile provide deterministic profiling of Python programs. A profile is a set of statistics that describes how often and for how long various parts of the program executed. These statistics can be formatted into reports via the pstats module.

How do you use Pyinstrument in python?

Call Pyinstrument directly from the command line. Instead of writing python script.py , type pyinstrument script.py . Your script will run as normal, and at the end (or when you press ^C ), Pyinstrument will output a colored summary showing where most of the time was spent.

How do you use line profiler in python?

The line_profiler test cases (found on GitHub) have an example of how to generate profile data from within a Python script. You have to wrap the function that you want to profile and then call the wrapper passing any desired function arguments. Also, you can add additional functions to be profiled as well.

How do you run a memory profiling in python?

The easiest way to profile a single method or function is the open source memory-profiler package. It's similar to line_profiler , which I've written about before . You can use it by putting the @profile decorator around any function or method and running python -m memory_profiler myscript.


2 Answers

I use py-spy with speedscope now. It is a very cool combination.

speedscope-view-of-py-spy-data

py-spy works on Windows/Linux/macOS, can output flame graphs by its own and is actively deployed, eg. subprocess profiling support was added in October 2019.

like image 84
guettli Avatar answered Oct 15 '22 22:10

guettli


Have you tried Pyflame? It's based on ptrace, so it shouldn't be affected by CPython's signal handling subtleties.

like image 40
Marius Gedminas Avatar answered Oct 15 '22 20:10

Marius Gedminas