|
1 | 1 | --- |
2 | 2 | title: "Python" |
3 | | -menuTitle: "Python (Coming Soon)" |
4 | | -description: "" |
| 3 | +menuTitle: "Python" |
| 4 | +description: "Instrumenting Pyhton applications for continuous profiling" |
5 | 5 | weight: 50 |
6 | 6 | --- |
7 | 7 |
|
8 | | -Coming Soon. |
| 8 | +# Python |
9 | 9 |
|
10 | | -If you're interested into Python integration, please reach out to us on [github](https://github.com/grafana/phlare/discussions/293). |
| 10 | +The Python module [pypprof] adds HTTP-based endpoints similar like Go's [`net/http/pprof`] for collecting profiles from a Python application. |
| 11 | + |
| 12 | +Under the hood, it uses [zprofile] and [mprofile] to collect CPU and heap profiles with minimal overhead. |
| 13 | + |
| 14 | +[`net/http/pprof`]: https://golang.org/pkg/net/http/pprof/ |
| 15 | +[pypprof]: https://github.com/timpalpant/pypprof |
| 16 | +[zprofile]: https://github.com/timpalpant/zprofile |
| 17 | +[mprofile]: https://github.com/timpalpant/mprofile |
| 18 | + |
| 19 | +## How to instrument your application |
| 20 | + |
| 21 | +First of all required Python modules need to be installed: |
| 22 | + |
| 23 | +```shell |
| 24 | +# Add the modules to the requirements.txt file |
| 25 | +cat >> requirements.txt <<EOF |
| 26 | +mprofile==0.0.14 |
| 27 | +protobuf==3.20.3 |
| 28 | +pypprof==0.0.1 |
| 29 | +six==1.16.0 |
| 30 | +zprofile==1.0.12 |
| 31 | +EOF |
| 32 | + |
| 33 | +# Build the module's wheels locally |
| 34 | +pip3 wheel --wheel-dir=/tmp/wheels -r requirements.txt |
| 35 | + |
| 36 | +# Install the modules |
| 37 | +pip3 install --no-index --find-links=/tmp/wheels -r requirements.txt |
| 38 | +``` |
| 39 | + |
| 40 | +Now the initialization code of your application should be invoking the web server exposing the profiling data: |
| 41 | + |
| 42 | +```python |
| 43 | +# import continous profiling modules |
| 44 | +from pypprof.net_http import start_pprof_server |
| 45 | +import mprofile |
| 46 | + |
| 47 | +# start memory profiling |
| 48 | +mprofile.start(sample_rate=128 * 1024) |
| 49 | + |
| 50 | +# enable pprof http server |
| 51 | +start_pprof_server(host='0.0.0.0', port=8081) |
| 52 | +``` |
| 53 | + |
| 54 | +To test the handlers you can use the [pprof] tool: |
| 55 | + |
| 56 | +```shell |
| 57 | +# Profile the current heap memory usage |
| 58 | +pprof -http :6060 "http://127.0.0.1:8081/debug/pprof/heap" |
| 59 | + |
| 60 | +# Profile the cpu for 5 seconds |
| 61 | +pprof -http :6060 "http://127.0.0.1:8081/debug/pprof/profile?seconds=5" |
| 62 | +``` |
| 63 | + |
| 64 | +[pprof]: https://github.com/google/pprof |
0 commit comments